Was talking with @jdalton tonight at the Seattle JS meetup about lodash (his presentation on it will hopefully be available soon), and how it unexpectedly creams the performance of the native versions of the functions it implements.
Some of lodash’s performance gains are due to optimizations made possible by some ugly hacks, and we were discussing how much more effectively these optimizations could be done if the underlying characteristics lodash detects heuristically were explicitly surfaced in JavaScript.
These APIs would pave the way for lodash to be more consistent across environments, and even make it easier to write more performant implementations for core functions in JavaScript (since context switches are, to my knowledge, one of the largest sources of perf losses, which is probably a big part of why stuff like lodash has better perf than native).
##Function#isBound
lodash has a (significant) performance boost where it checks whether a function uses this
to determine if it can use f()
instead of f.bind()
/f.call()
. Right now jdalton has this shimmed with /\bthis\b/.test(f.toString())
, which is very hacky (with some environments not even supporting function stringification at all). If this were made available, lodash could just check f.isBound
.
Function#isNative
The way lodash determines this right now is by comparing with something like Object.prototype.toString.toString()
, which is only slightly less unreliable than the above hack for testing the existence of a reference.
I think there was also some mention of surfacing a flag for native objects (eg. DOM elements) as well.
Array#isSparse
lodash is currently designed to assume that an array is never sparse. Exposing this would let lodash implement a slow-path for sparse array compatibility, and subsequently / conversely open the door for performant shims of existing sparse-array-compatible core functions.
cloneObject
I’m not sure where this could go or what the interface / signature should be, but jdalton mentioned that his work would be significantly easier (as it would certainly be for many other developers, myself included) if VMs exposed the deep object clone method they use for places like postMessage
.
(Edit: replaced Function#references with Function#isBound per @FremyCompany’s suggestion)