From 25616f3ad2132b9dd7d06aba8c03ad7d8fb97f12 Mon Sep 17 00:00:00 2001 From: "vassili.gorshkov" Date: Thu, 7 Sep 2017 17:54:24 -0700 Subject: [PATCH 1/2] Improved performance by the factor of 10 for the case of a lookup $.a.b.c The change is simple caching of the parse(), normalize() and strigify() functions --- lib/index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 8f5a832..67ce158 100755 --- a/lib/index.js +++ b/lib/index.js @@ -2,6 +2,7 @@ var assert = require('assert'); var dict = require('./dict'); var Parser = require('./parser'); var Handlers = require('./handlers'); +var _ = require('underscore'); var JSONPath = function() { this.initialize.apply(this, arguments); @@ -9,6 +10,7 @@ var JSONPath = function() { JSONPath.prototype.initialize = function() { this.parser = new Parser(); + this.parser.parse = _.memoize(this.parser.parse); this.handlers = new Handlers(); }; @@ -152,7 +154,7 @@ JSONPath.prototype.nodes = function(obj, string, count) { return count ? matches.slice(0, count) : matches; }; -JSONPath.prototype.stringify = function(path) { +JSONPath.prototype.stringify = _.memoize(function(path) { assert.ok(path, "we need a path"); @@ -187,9 +189,9 @@ JSONPath.prototype.stringify = function(path) { }); return string; -} +}); -JSONPath.prototype._normalize = function(path) { +JSONPath.prototype._normalize = _.memoize(function(path) { assert.ok(path, "we need a path"); @@ -234,7 +236,7 @@ JSONPath.prototype._normalize = function(path) { } throw new Error("couldn't understand path " + path); -} +}); function _is_string(obj) { return Object.prototype.toString.call(obj) == '[object String]'; From fbf542f3130bbf319783bd5a0a434e45786f854d Mon Sep 17 00:00:00 2001 From: "vassili.gorshkov" Date: Fri, 8 Sep 2017 12:22:50 -0700 Subject: [PATCH 2/2] squash --- lib/index.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index 67ce158..8f5a832 100755 --- a/lib/index.js +++ b/lib/index.js @@ -2,7 +2,6 @@ var assert = require('assert'); var dict = require('./dict'); var Parser = require('./parser'); var Handlers = require('./handlers'); -var _ = require('underscore'); var JSONPath = function() { this.initialize.apply(this, arguments); @@ -10,7 +9,6 @@ var JSONPath = function() { JSONPath.prototype.initialize = function() { this.parser = new Parser(); - this.parser.parse = _.memoize(this.parser.parse); this.handlers = new Handlers(); }; @@ -154,7 +152,7 @@ JSONPath.prototype.nodes = function(obj, string, count) { return count ? matches.slice(0, count) : matches; }; -JSONPath.prototype.stringify = _.memoize(function(path) { +JSONPath.prototype.stringify = function(path) { assert.ok(path, "we need a path"); @@ -189,9 +187,9 @@ JSONPath.prototype.stringify = _.memoize(function(path) { }); return string; -}); +} -JSONPath.prototype._normalize = _.memoize(function(path) { +JSONPath.prototype._normalize = function(path) { assert.ok(path, "we need a path"); @@ -236,7 +234,7 @@ JSONPath.prototype._normalize = _.memoize(function(path) { } throw new Error("couldn't understand path " + path); -}); +} function _is_string(obj) { return Object.prototype.toString.call(obj) == '[object String]';