Hacker News new | ask | show | jobs
by Waterluvian 1209 days ago
This is delightful!

Maybe next you can make the object spread operator faster ;). I was recently bitten by it in a hot loop, thinking it was generally syntactic sugar. Turns out that if I know all the property names in an object, it’s far far faster to just assign them all manually.

3 comments

That could be interesting! Curious: are you taking about a plain object spread, or are you talking about JSX spreads? The latter has more overhead. Also, which JS engine?

BTW. I recently measured the spread operator in a microbenchmark. Interestingly, while the native spread was faster on Hermes and JSC, it was slower on V8/Chrome, and it's faster there to use `Object.assign()`, i.e.:

['@babel/plugin-proposal-object-rest-spread', { loose: true, useBuiltIns: true }]

Doesn’t assigning properties behave differently? Spread creates a new object while assigning reuses the existing. Unless you mean creating a new object and then assigning the properties. I’d be surprised if that performed worse though. Also curious if the properties are primitives?
I wonder if you can just configure this as part of the compile step with typescript, when the object you're spreading has a known type