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!

 

fastJavascript

 

 

 

 

 

 

 

 

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. 

 

fastJavascript

 

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); }
arr.forEach(logIt);

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. 

 

fastJavascript

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.

 

Banner


Firefox 1.0 Released 20 Years Ago
10/11/2024

A news item with the headline "Firefox browser takes on Microsoft" from 20 years ago has attracted renewed attention. It was originally published on the BBC News website on November 9th, 2004 rec [ ... ]



AI Breakthrough For Robot Surgery
17/11/2024

Using imitation learning, a robot has learned to perform surgical procedures as skillfully as human surgeons, bringing the field of robotic surgery closer to true autonomy.


More News

 

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 

Last Updated ( Tuesday, 01 July 2014 )