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]';