Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

Commit 3b6a42a

Browse files
committed
Memoize results of parsing, normalization, and stringification, per dchester#75
1 parent 9713c6f commit 3b6a42a

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

lib/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ var assert = require('assert');
22
var dict = require('./dict');
33
var Parser = require('./parser');
44
var Handlers = require('./handlers');
5+
var _ = require('underscore');
56

67
var JSONPath = function() {
78
this.initialize.apply(this, arguments);
89
};
910

1011
JSONPath.prototype.initialize = function() {
1112
this.parser = new Parser();
13+
this.parser.parse = _.memoize(this.parser.parse);
1214
this.handlers = new Handlers();
1315
};
1416

@@ -152,7 +154,7 @@ JSONPath.prototype.nodes = function(obj, string, count) {
152154
return count ? matches.slice(0, count) : matches;
153155
};
154156

155-
JSONPath.prototype.stringify = function(path) {
157+
JSONPath.prototype.stringify = _.memoize(function(path) {
156158

157159
assert.ok(path, "we need a path");
158160

@@ -187,9 +189,9 @@ JSONPath.prototype.stringify = function(path) {
187189
});
188190

189191
return string;
190-
}
192+
});
191193

192-
JSONPath.prototype._normalize = function(path) {
194+
JSONPath.prototype._normalize = _.memoize(function(path) {
193195

194196
assert.ok(path, "we need a path");
195197

@@ -234,7 +236,7 @@ JSONPath.prototype._normalize = function(path) {
234236
}
235237

236238
throw new Error("couldn't understand path " + path);
237-
}
239+
});
238240

239241
function _is_string(obj) {
240242
return Object.prototype.toString.call(obj) == '[object String]';

0 commit comments

Comments
 (0)