6

According to this answer to 'Is object empty?':

// Speed up calls to hasOwnProperty
var hasOwnProperty = Object.prototype.hasOwnProperty;

I've seen several implementations of something similar in small JavaScript libraries, like:

var slice = Array.prototype.slice;

//or

function slice(collection) {
    return Array.prototype.slice.call(collection);
}

I did a quick jsperf to test this sort of thing, and caching looked a bit quicker overall than not caching, but my test could be flawed.

(I am using the word 'cache' to mean storing the method inside a variable.)

The context of this question is when a developer needs to call the native method multiple times, and what the observable difference would be.

Does caching the native method prevent the engine from having to look inside the object for the method every time the method is called, thus making caching a faster way to call native methods whenever the developer needs to call the same native method more than once?

Community
  • 1
  • 1
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • 1
    [Is micro-optimization worth the time?](http://stackoverflow.com/a/3471000/402037) – Andreas Feb 12 '14 at 22:05
  • What makes you think this is related to nativeness of methods? The part that's being optimized, repeated attribute lookup, should be completely agnostic w.r.t. what's being looked up (in fact, I can't imagine a non-contrieved scheme where it possibly could make a difference). –  Feb 12 '14 at 22:05
  • What this is saving is searching through a chain of nested prototypes for the property. – Barmar Feb 12 '14 at 22:06
  • @delnan Perhaps the impact of the optimization is more significant for simple, native methods, because their implementation is fast and the name lookup is a bigger percentage of their calling time. – Barmar Feb 12 '14 at 22:08
  • Looking at those jsPerf results, the performance gain is virtually nonexistent. – Peter Feb 12 '14 at 22:15

2 Answers2

4

When you're using Array.prototype.slice a lot in, say, a library, it makes sense to create a variable holding that function (var slice = Array.prototype.slice;) because the variable can be minified by a JavaScript minifier which it can't otherwise.

Assigning the function to a variable also avoids having to traverse the object's prototype chain, which might result in a slightly better performance.

Note that this is micro-optimization, which you (generally speaking) shouldn't concern yourself too much with – leave that up to a modern JavaScript engine.

Marius Schulz
  • 15,976
  • 12
  • 63
  • 97
  • GZIP defeats the minification benefits of using a variable to reference symbols, and doing so can actually increase the size after GZIP. The performance benefits of not having to perform property lookups are valid however. – Alexander O'Mara Jan 11 '16 at 03:25
1

Saving the value in a variable presents some optimization opportunities because if its a local variable the interpreter could do analyisis to realize that the vatiable never gets mutated. On the other hand, you always need to dereference globals like Array, since everyone could potentially change them at any time.

That said, I have no idea if this is going to matter for performance, expecially once you consider the JIT optimizations.

Usually, the biggest reason people use var slice is to keep the source code short.

hugomg
  • 68,213
  • 24
  • 160
  • 246