From 934d7ebb5aebcf3ac7069cf66874c5c51e44897c Mon Sep 17 00:00:00 2001 From: Patrick Delcroix Date: Tue, 11 Jul 2023 22:48:45 +0200 Subject: [PATCH 1/3] use map instead of object it seems that maps are faster, as this is assessed many time small speed improvement can have good effect https://www.zhenghao.io/posts/object-vs-map --- src/js/form-model.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/js/form-model.js b/src/js/form-model.js index a2252c1f..3f028480 100644 --- a/src/js/form-model.js +++ b/src/js/form-model.js @@ -50,7 +50,7 @@ const FormModel = function (data, options) { options.full = typeof options.full !== 'undefined' ? options.full : true; this.events = document.createElement('div'); - this.convertedExpressions = {}; + this.convertedExpressions = new Map(); this.templates = {}; this.loadErrors = []; @@ -1424,7 +1424,7 @@ FormModel.prototype.evaluate = function ( cacheable = original === expr; // if no cached conversion exists - if (!this.convertedExpressions[cacheKey]) { + if (!this.convertedExpressions.has(cacheKey)) { expr = expr.trim(); expr = this.replaceInstanceFn(expr); expr = this.replaceVersionFn(expr); @@ -1440,10 +1440,10 @@ FormModel.prototype.evaluate = function ( expr = expr.replace(/>/g, '>'); expr = expr.replace(/"/g, '"'); if (cacheable) { - this.convertedExpressions[cacheKey] = expr; + this.convertedExpressions.set(cacheKey,expr); } } else { - expr = this.convertedExpressions[cacheKey]; + expr = this.convertedExpressions.get(cacheKey); } resultTypes = { From 1c7385b558a0449daa30e48fac5e1b85b2e4f44d Mon Sep 17 00:00:00 2001 From: Patrick Delcroix Date: Wed, 12 Jul 2023 08:46:05 +0200 Subject: [PATCH 2/3] access data only once --- src/js/form-model.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/js/form-model.js b/src/js/form-model.js index 3f028480..d7138772 100644 --- a/src/js/form-model.js +++ b/src/js/form-model.js @@ -1424,7 +1424,8 @@ FormModel.prototype.evaluate = function ( cacheable = original === expr; // if no cached conversion exists - if (!this.convertedExpressions.has(cacheKey)) { + cached_expr = this.convertedExpressions.get(cacheKey) + if (cached_expr === undefined) { expr = expr.trim(); expr = this.replaceInstanceFn(expr); expr = this.replaceVersionFn(expr); @@ -1443,7 +1444,7 @@ FormModel.prototype.evaluate = function ( this.convertedExpressions.set(cacheKey,expr); } } else { - expr = this.convertedExpressions.get(cacheKey); + expr = cached_expr; } resultTypes = { From 64a73f99a3e7b05c5afe92abaeb4b96373c5c00b Mon Sep 17 00:00:00 2001 From: eyelidlessness Date: Fri, 4 Aug 2023 10:48:59 -0700 Subject: [PATCH 3/3] Address lint errors --- src/js/form-model.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/js/form-model.js b/src/js/form-model.js index d7138772..e216e5a5 100644 --- a/src/js/form-model.js +++ b/src/js/form-model.js @@ -1424,8 +1424,9 @@ FormModel.prototype.evaluate = function ( cacheable = original === expr; // if no cached conversion exists - cached_expr = this.convertedExpressions.get(cacheKey) - if (cached_expr === undefined) { + const cachedExpr = this.convertedExpressions.get(cacheKey); + + if (cachedExpr === undefined) { expr = expr.trim(); expr = this.replaceInstanceFn(expr); expr = this.replaceVersionFn(expr); @@ -1440,11 +1441,12 @@ FormModel.prototype.evaluate = function ( expr = expr.replace(/</g, '<'); expr = expr.replace(/>/g, '>'); expr = expr.replace(/"/g, '"'); + if (cacheable) { - this.convertedExpressions.set(cacheKey,expr); + this.convertedExpressions.set(cacheKey, expr); } } else { - expr = cached_expr; + expr = cachedExpr; } resultTypes = {