Fast.js JavaScript Versions Of Built-In Functions Are Faster
Fast.js JavaScript Versions Of Built-In Functions Are Faster
Written by Ian Elliot   
Tuesday, 01 July 2014

Just to demonstrate how all grown up JavaScript is, Fast.js re-implements a number of built-in native functions in JavaScript. Guess what - they are faster!











There used to be a basic rule that in a language like JavaScript built-in functions ware always much faster than the equivalent code expressed in the raw language. The reason was fairly obvious - the built-in functions were coded in  native code, whatever that might be, and you were comparing compiled versus interpreted. However, over time and with advanced JIT compilers and optimization techniques, JavaScript has got faster. So fast that the trusted rule no longer seems to apply, or does it?


Fast.js is an open source library that provides implementations of .foreach, .map, .reduce, concat, bind, apply, filter, indexOf and LastIndexOf. It also implements two standard utility functions -  partial and clone, which are not built in - in a fast style  and it provides an alternative for try as a fast function. 




Some of the functions are simply faster versions, but some make simplifying assumptions to provide even more speed. For example, the built-in map, reduce and foreach functions have to cope with the possibility that the array that they are being used with is sparse.  The example given in the documentation is:

var arr = new Array(100);
// a sparse array with 100 slots
arr[20] = 'Hello World';
function logIt (item) { console.log(item); }

In this case there is only one used element in the array and the built-in function forEach only calls logIt once. The fast alternative assumes that all arrays are completely used, i.e. non-sparse, and it can avoid having to do the test that the built-in function has to perform. For example:

var fast = require('fast.js');
var arr = [1,2,3,4,5];
fast.forEach(arr, logIt);
// faster than arr.forEach(logIt

The payoff for not handling sparse arrays is that the fast function is up to five times faster. 

Other functions in the library are similarly optimized for the most common use cases - consult the documentation for more information.

You can also find a benchmark which you can use to see if it is worth using the fast versions in your production environment. The gains vary according to the function, but can be anything from a modest 30% to an unlikely sounding 3650% improvement. 



More Information

Fast.js On GitHub

Related Articles

Asm.js Gets Faster

Java, ASM.js Or Native - Which Is Faster?       

Octane 2.0 Released       

JavaScript Assembly Language       


To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.



2015 Source Code Poetry Competition

No this is not some vague philosophical point about code being so beautiful it is a logical poem - although it undoubtedly is - this is real poetry. It has to rhyme and it has to compile. 

Visual Studio V Android Studio

If you think that Microsoft's involvement with open source and even Linux is a remarkable reversal, what about Visual Studio supporting Java - and Android native development? Visual Studio is now an a [ ... ]

More News


blog comments powered by Disqus


Last Updated ( Tuesday, 01 July 2014 )

RSS feed of news items only
I Programmer News
Copyright © 2015 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.