Closed kurtextrem closed 1 month ago
Tried rebasing on the work I was doing in https://github.com/jshttp/cookie/pull/170. The alternative in https://github.com/jshttp/cookie/pull/170 was to do a while (index < length - 2)
check which would eliminate lines 4 & 5 from your list, as well as merges the new if
with the while
making it only 2/5. So at this point it's only eliminating the dec
line, but I'll merge anyway, I'd love to see more perf wins!
Thanks for the PR.
This PR caches
str.length
(since the string length stays the same across the function). In addition, I've also added an early return for cookie strings that are below the min. spec length and added a test for that.Explanation
Previously, for e.g. empty strings as input we did the following things:
var opt = options || {};
var dec = opt.decode || decode
while (index < str.length)
var eqIdx = str.indexOf('=', index)
if (eqIdx === -1) break
now it is only
if (length < 2) return
, so it saves us time on early return.do-while instead of while
Since we've taken care of the min string length, we can make the assumption there should always be one run of the statement.
less allocations
By reusing the
var
s, we produce less allocations and we don't allocate a{}
in case options are missing.benchmark