Skip to content

Commit 5239bc0

Browse files
mrmlncmichael-ciniawsky
authored andcommitted
perf(index): Create a copy only for specified keys
1 parent ba9d706 commit 5239bc0

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

lib/index.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ function walk (opts, nodes) {
181181
const keys = loopParams.keys
182182

183183
// creates a copy of the keys that will be changed within the loop
184-
const localsBackup = makeBackupLoopKeys(keys[0], keys[1], opts.locals)
184+
const localsBackup = makeLocalsBackup(keys, opts.locals)
185185

186186
// run the loop, different types of loops for arrays and objects
187187
if (Array.isArray(target)) {
@@ -195,7 +195,7 @@ function walk (opts, nodes) {
195195
}
196196

197197
// returns the original keys values that was changed within the loop
198-
opts.locals = revertBackupedLocals(keys[0], keys[1], opts.locals, localsBackup)
198+
opts.locals = revertBackupedLocals(keys, opts.locals, localsBackup)
199199

200200
// return directly out of the loop, which will skip the "each" tag
201201
return m
@@ -269,27 +269,31 @@ function parseLoopStatement (input) {
269269
}
270270

271271
/**
272-
* Creates a backup of keys within the loop
272+
* Creates a backup of keys values
273273
*/
274-
function makeBackupLoopKeys (p1, p2, locals) {
274+
function makeLocalsBackup (keys, locals) {
275275
let backup = {}
276-
if (p1 && data.locals.hasOwnProperty(p1)) backup[p1] = data.locals[p1]
277-
if (p2 && data.locals.hasOwnProperty(p2)) backup[p2] = data.locals[p2]
276+
277+
for (let i = 0; i < keys.length; i++) {
278+
const key = keys[i]
279+
if (data.locals.hasOwnProperty(key)) backup[key] = cloneDeep(data.locals[key])
280+
}
278281

279282
return backup
280283
}
281284

282285
/**
283286
* Returns the original keys values
284287
*/
285-
function revertBackupedLocals (p1, p2, locals, backup) {
286-
// Remove loop keys from locals
287-
delete locals[p1]
288-
delete locals[p2]
289-
290-
// Revert copied keys
291-
if (p1 && backup.hasOwnProperty(p1)) locals[p1] = backup[p1]
292-
if (p2 && backup.hasOwnProperty(p2)) locals[p2] = backup[p2]
288+
function revertBackupedLocals (keys, locals, backup) {
289+
for (let i = 0; i < keys.length; i++) {
290+
const key = keys[i]
291+
// remove key from locals
292+
delete locals[key]
293+
294+
// revert copied key value
295+
if (backup.hasOwnProperty(key)) locals[key] = backup[key]
296+
}
293297

294298
return locals
295299
}

0 commit comments

Comments
 (0)