diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py index 364e44d40cc307..42caf1f8fada9b 100644 --- a/Lib/json/decoder.py +++ b/Lib/json/decoder.py @@ -355,10 +355,16 @@ def decode(self, s, _w=WHITESPACE.match): containing a JSON document). """ - obj, end = self.raw_decode(s, idx=_w(s, 0).end()) - end = _w(s, end).end() + # Skip the WHITESPACE.match() call (and its match-object allocation) + # for the common case where there is no leading whitespace. + idx = _w(s, 0).end() if s and s[0] in ' \t\n\r' else 0 + obj, end = self.raw_decode(s, idx=idx) + # Likewise avoid the trailing-whitespace match when the parse already + # consumed the whole string. if end != len(s): - raise JSONDecodeError("Extra data", s, end) + end = _w(s, end).end() + if end != len(s): + raise JSONDecodeError("Extra data", s, end) return obj def raw_decode(self, s, idx=0): diff --git a/Misc/NEWS.d/next/Library/2026-06-03-07-31-11.gh-issue-150860.8VbmHU.rst b/Misc/NEWS.d/next/Library/2026-06-03-07-31-11.gh-issue-150860.8VbmHU.rst new file mode 100644 index 00000000000000..f1bf6c7070d4e5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-03-07-31-11.gh-issue-150860.8VbmHU.rst @@ -0,0 +1,3 @@ +Speed up :func:`json.loads` for documents without leading or trailing +whitespace by skipping the whitespace scan in that common case. Patch by Bernát +Gábor.