|Written by Ian Elliot|
|Tuesday, 01 July 2014|
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:
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:
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.
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
or email your comment to: firstname.lastname@example.org
|Last Updated ( Tuesday, 01 July 2014 )|