@@ -2,13 +2,15 @@ var assert = require('assert');
22var dict = require ( './dict' ) ;
33var Parser = require ( './parser' ) ;
44var Handlers = require ( './handlers' ) ;
5+ var _ = require ( 'underscore' ) ;
56
67var JSONPath = function ( ) {
78 this . initialize . apply ( this , arguments ) ;
89} ;
910
1011JSONPath . 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
239241function _is_string ( obj ) {
240242 return Object . prototype . toString . call ( obj ) == '[object String]' ;
0 commit comments