I disagree about call and apply though. I prefer to never use:
new
this (one exception)
Object.create
bind
call
apply
When I avoid this list my code tends to be smaller and faster to execute. You don't need OOP in this language, thanks to lexical scope, to achieve decomposable reuse and reference sharing.
The one exception for this is event handlers where it is commonly necessary to access the event target without access to an explicit reference.
Is it really? You have a click event on a button. If you are able to click that button a thousand times a second, I might see a reason to care about a few clock ticks of performance, but for the use case bind provides an easy readable way to implement event binding.
Oh, I can name you a few times it's worth using bind. Currying, for instance. But you should avoid bind unless you need it. Simply using it to permanently set scope is not a good usage; use an anonymous call (apparently not apply!!) for that.
It is strange that apply is slow; I would think that would be optimized better than call.
function sub_curry(fn /*, variable number of args */) {
var args = [].slice.call(arguments, 1);
return function () {
return fn.apply(this, args.concat(toArray(arguments)));
};
}
It completely agree it is definitely worth using bind to ensure that this is commonly understood and doesn't get lost. If you don't use this, though, you don't need bind.
Bind and apply are not likely to significantly impact performance enough to measure. It is not for performance reasons that I avoid these. I don't use them is because they are unnecessary.
Coming from a C++ like language you absolutely need this stuff. You need classes and a way to extend those classes or reference an object property in relation to a known context. Each of these features requires code definitions (overhead). A code author has to manually write code to define points of inheritance and bindings that connect various objects together and establish the organization of the code.
C++ has been around since 1982 and did not get lexical scope until 2008 (i think). Java did not get lexical scope until April 2014 (hasn't even been a year). A functional approach to writing code is available to these languages now, but it has not always been that way. Developer who grew up in those language only know the OO way that requires lots of code overhead. The OO way is not absolutely necessary any more in these languages, but this line of thinking is institutionalized.
JavaScript had lexical scope in 1995 when it was born. The advantage to lexical scope is that you don't have to define any of the organizing facets. The inheritance model is simply understood by the language directly from nesting of scopes. The way it works is if a reference is defined in an outer scope it is available (immediately) to an inner scope. You just directly call the outer reference as though it were local to the nested scope. The advantage is that you can still establish a public/private model of organization, reuse and sharing, and inheritance (of scope) without doing anything more than nesting the scopes.
The lexical scope chain is the primary means of reference resolution in JavaScript and the prototype chain is a secondary means of reference resolution. When a reference is encountered (at execution time) it must be resolved, which means finding where it was declared and what its value is. The lexical scope chain is searched first and if the reference cannot be found the prototype chain is searched. The total difference in performance is likely negligible because the JIT engine will attempt to cache the declaration point as much as possible. The primary performance difference is noticed from having less code to execute (and fewer references) to achieve the same result.
My thoughts are if I can get away with writing (and maintaining) substantially less code then any performance benefits are merely serendipity. Fortunately, writing less code happens to mean faster execution almost universally.
-1
u/[deleted] Mar 09 '15
I disagree about call and apply though. I prefer to never use:
new
this (one exception)
Object.create
bind
call
apply
When I avoid this list my code tends to be smaller and faster to execute. You don't need OOP in this language, thanks to lexical scope, to achieve decomposable reuse and reference sharing.
The one exception for this is event handlers where it is commonly necessary to access the event target without access to an explicit reference.