From c4372cdb3829138a1e5287e1c4ef8dc9c6c5b5a5 Mon Sep 17 00:00:00 2001 From: Alexander Dutton Date: Sun, 15 Feb 2015 20:06:05 +0000 Subject: Optimize in get_part for the common cases of doc being dict or list --- jsonpointer.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/jsonpointer.py b/jsonpointer.py index 29a71f5..3e8b31c 100644 --- a/jsonpointer.py +++ b/jsonpointer.py @@ -196,7 +196,12 @@ class JsonPointer(object): def get_part(self, doc, part): """ Returns the next step in the correct type """ - if isinstance(doc, Sequence): + # Optimize for common cases of doc being a dict or list, but not a + # sub-class (because isinstance() is far slower) + ptype = type(doc) + if ptype == dict: + return part + if ptype == list or isinstance(doc, Sequence): if part == '-': return part if not RE_ARRAY_INDEX.match(str(part)): -- cgit v1.2.1