2 ms·
It's not quadratic if the way reduce() and JS inlining work together causes this: return prev + DOMPurify.sanitize(mastodonComment); to end up behaving li
by jlokier 6y ago
It's not quadratic if the way reduce() and JS inlining work together causes this:
return prev + DOMPurify.sanitize(mastodonComment);
to end up behaving like this:
reduceValue = reduceValue + DOMPurify.sanitize(mastodonComment);
which ends up behaving like this:
reduceValue += DOMPurify.sanitize(mastodonComment);
which ends up being a sequence of in-place string appends.
In any good implementation, a sequence of repeated appends takes linear time in the total length of all the appended strings, like join('').
I've just tested this theory in Safari 14.0.2 with a JS console one-liner. On this browser, both versions take linear time not quadratic, and the test corresponding to the first version even runs slightly faster.
The time difference is small though, and you're right, the second version is explicitly linear time. If I had tested with a much older browser (maybe IE8), I think the first version would have taken quadratic time.