Reputation:
Is this simply to shorten the lookup chain?
var slice = Array.prototype.slice;
var splice = Array.prototype.splice;
http://backbonejs.org/docs/backbone.html
jQuery does something similar:
core_push = Array.prototype.push,
core_slice = Array.prototype.slice,
http://code.jquery.com/jquery-1.8.2.js
Upvotes: 1
Views: 149
Reputation: 1110
The most performant way to slice,splice and push is going to use C code. Since all the browsers are written in C using Array.splice will use C internally instead of JS processing.
If you really need the performance you should try to find a way to use neither. The issue is they will both create zombie objects that will be laying around for the garbage collector to get later on. The less work the GC does the more time you will save in the long run. Use the same array and edit at different positions in the array using a cursor. This will save the GC from collecting it.
There's an example here: https://github.com/puppybits/BackboneJS-PerfView/blob/master/PerfView.js on line 40 in the trackFPS function. If you're pushing/slicing a lot and need the performance consider a static array.
Upvotes: 0
Reputation: 153284
For Backbone, that makes no sense at all. Both slice
and splice
are used exactly once, thus the var
declarations crate unnecessary overhead.
For jQuery, different story. A local reference of something that is referenced numerous times, facilitates minification. So the code size of the production version decreases.
Yes, it's a minor performance gain too, but nothing worth talking about.
Upvotes: 2