4 ms·
Direct assignment without using the calculated length property performs way ahead than others, at least in chrome http://jsperf.com/array-direct-assignment-vs-
by suhair 13y ago
Direct assignment without using the calculated length property performs way ahead than others, at least in chrome http://jsperf.com/array-direct-assignment-vs-push/3 http://jsperf.com/array-direct-assignment-vs-push/3
- willurd 13y agoWow, direct assignment is an order of magnitude faster than push (in my test, chrome 27 on windows 7). Can anyone explain why?
- mischanix 13y agoDirect assignment is the only test in which the length of array doesn't change.
- kwamenum86 13y agothis smells like a js engine optimization. you wouldn't expect substantially different machine code to run for "Direct assignment" and "other". i wouldn't be surprised if the loop is being unrolled or something.
- lttlrck 13y agoThey could all be reasonably unrolled, so it's probably something else. I expect the engine can more easily calculate the final required Array size and preallocate it upfront avoiding resize operation on each loop iteration. (which is anyway JS best practice when possible)
- deleted 13y ago[deleted]