Short Walk

Shortens the length of the prototype chain walk.

jQuery assigns frequently used global functions to local variables to speed up processing.



Overview


Within the Single var area in jQuery there are several assignments of built-in JavaScript functions to local variables. Here is an extract:

core_push = Array.prototype.push,
core_slice = Array.prototype.slice,
core_indexOf = Array.prototype.indexOf,
core_toString = Object.prototype.toString,
core_hasOwn = Object.prototype.hasOwnProperty,
core_trim = String.prototype.trim,

These are implementations of the Short Walk idiom which is designed to speed up processing. Having these methods available as local variables will prevent the JavaScript engine from having to walk the scope chain every time these methods are needed.

A slight disadvantage is that you need to invoke these with call or apply, making the code a bit less legible:

core_push.call( ret, arr );

core_indexOf.call( arr, elem, i );

core_toString.call(obj)

These lines are all from jQuery.


Minification
The above Short Walk idiom does more than provide faster access. It also helps in minifying the code. The local variables can get shortened to 1 or 2 character names, which would not be possible with built-in methods with longer names and object chains. This is the Minification Idiom.