Skip to content

Commit 1046bc4

Browse files
mrmlncmichael-ciniawsky
authored andcommitted
perf(index): Optimize scoped locals for scope tag
1 parent 5239bc0 commit 1046bc4

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

lib/index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const vm = require('vm')
4-
const merge = require('lodash.merge')
54
const cloneDeep = require('lodash.clonedeep')
65
const placeholders = require('./placeholders')
76

@@ -215,7 +214,15 @@ function walk (opts, nodes) {
215214
throw new Error('You must provide an object to make scope')
216215
}
217216

218-
m.push(executeScoped(target, node))
217+
const keys = Object.keys(target)
218+
219+
// creates a copy of the keys that will be changed within the loop
220+
const localsBackup = makeLocalsBackup(keys, opts.locals)
221+
222+
m.push(executeScoped(target, opts.locals, node))
223+
224+
// returns the original keys values that was changed within the loop
225+
opts.locals = revertBackupedLocals(keys, opts.locals, localsBackup)
219226

220227
// return directly out of the loop, which will skip the "scope" tag
221228
return m
@@ -316,11 +323,7 @@ function executeLoop (loopParams, p1, p2, locals, treeString) {
316323
/**
317324
* Runs walk function with arbitrary set of local variables
318325
*/
319-
function executeScoped (scopedLocals, node) {
320-
// merge nondestructively into existing locals
321-
const scopedOptions = merge(cloneDeep(data), { locals: scopedLocals })
322-
// walk through the contents and run replacements with modified options
323-
// we need to clone the node because the normal operation modifies
324-
// the node directly
325-
return walk(scopedOptions, cloneDeep(node.content))
326+
function executeScoped (scopeLocals, locals, node) {
327+
const scopedLocals = Object.assign(locals, scopeLocals)
328+
return walk({ locals: scopedLocals }, node.content)
326329
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
"url": "https://github.com/posthtml/posthtml-expressions/issues"
1111
},
1212
"dependencies": {
13-
"lodash.clonedeep": "^4.3.2",
14-
"lodash.merge": "^4.4.0"
13+
"lodash.clonedeep": "^4.3.2"
1514
},
1615
"devDependencies": {
1716
"ava": "^0.16.0",

0 commit comments

Comments
 (0)