javascript - Underscore.js wrapped callbacks efficiency -



javascript - Underscore.js wrapped callbacks efficiency -

i have question annotated underscore.js source code:

it says:

internal function returns efficient (for current engines) version of passed-in callback, repeatedly applied in other underscore functions.

var createcallback = function(func, context, argcount) { if (context === void 0) homecoming func; switch (argcount == null ? 3 : argcount) { case 1: homecoming function(value) { homecoming func.call(context, value); }; case 2: homecoming function(value, other) { homecoming func.call(context, value, other); }; case 3: homecoming function(value, index, collection) { homecoming func.call(context, value, index, collection); }; case 4: homecoming function(accumulator, value, index, collection) { homecoming func.call(context, accumulator, value, index, collection); }; } homecoming function() { homecoming func.apply(context, arguments); }; };

explicitly, how reached efficiency in block of code ?

cause see wrapper, created 1 more level of indirection , scope chain grows 1 more level two.

i need understand how reached efficiency, in order apply tricks in own js.

thanks!

there seems shuffling of argument order of cases equivalent if later arguments left empty. think logic makes callback play nicely browser optimisations. example, if homecoming value predictable result may inlined rather calling actual function each time. here related give-and-take performance , arguments, testing showed big speed differences:

performance penalty undefined arguments

javascript performance underscore.js

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -