From d3bb5ae16f0d653c5d5145e7874ada06bfb63160 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Sat, 25 Aug 2018 17:03:03 -0700 Subject: [PATCH] Replace object with Map for performance --- lib/session.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/session.js b/lib/session.js index 55e4bc6..51d72c0 100644 --- a/lib/session.js +++ b/lib/session.js @@ -1817,7 +1817,7 @@ function processDeclarations( function func(leafParam, leafIsObject, leafTimestamp, _paths) { const currentPath = _paths[0]; - const children = {}; + const children = new Map(); let declareTimestamp = 0; let declareAttributeTimestamps; let declareAttributeValues; @@ -1830,15 +1830,18 @@ function processDeclarations( for (const path of _paths) { if (path.length > currentPath.length) { const fragment = path[currentPath.length]; - if (!children[fragment]) { - children[fragment] = []; + let child = children.get(fragment); + if (!child) { if (path.length > currentPath.length + 1) { // This is to ensure we don't descend more than one step at a time const p = common.addPathMeta(path.slice(0, currentPath.length + 1)); - children[fragment].push(p); + child = [p]; + } else { + child = []; } + children.set(fragment, child); } - children[fragment].push(path); + child.push(path); continue; } @@ -2041,18 +2044,19 @@ function processDeclarations( } } - for (const child of Object.keys(children)) { + for (let [fragment, child] of children) { // This fine expression avoids duplicate visits, don't ask. if ( - ((currentPath.wildcard ^ children[child][0].wildcard) & + ((currentPath.wildcard ^ child[0].wildcard) & ((1 << currentPath.length) - 1)) >> leafParam.length === 0 ) { - if (child !== "*" && children["*"]) - children[child] = children[child].concat(children["*"]); - - func(leafParam, leafIsObject, leafTimestamp, children[child]); + if (fragment !== "*") { + const wildcardChild = children.get("*"); + if (wildcardChild) child = child.concat(wildcardChild); + } + func(leafParam, leafIsObject, leafTimestamp, child); } } }