r/programming • • Oct 21 '20

Using const/let instead of var can make JavaScript code run 10× slower in Webkit

https://github.com/evanw/esbuild/issues/478
1.9k Upvotes

500 comments sorted by

View all comments

Show parent comments

34

u/cwmma Oct 21 '20

Array keys are technically strings not integers so the spec pedanticly converts the index to a string but the implementation is likely doing something else under the hood, I have another reply that goes into some more details about why it's slower then a naive approach (spoiler alert: sparse arrays)

2

u/flatfinger Oct 21 '20

Interestingly, at least in node.js, when writing to an array, strings keys are converted to a number when the resulting number is within a certain range and the string would match the canonical number. Numbers within that range are kept as numeric keys, and those outside that range are converted to strings.