Use ES5 Features Now

I’ve come to the world of JavaScript programming late, and one thing that really bugs me is the lack of use of ECMAScript 5 in many packages. I understand that people need backwards compatibility, but many of these things can be shimmed. It’s time to make the older browsers run more slowly, so the new ones can run faster.

When I look at the list of additions below, I can only wonder what took so long for these things to get there! The list below are things that are known to shim well. (You can use the ES5 shim, or use the polyfills available in the Mozilla Developer Network links below.)

Array.isArray

Date.now
Date.prototype.toISOString
Date.prototype.toJSON

JSON.parse
JSON.stringify
(The two JSON methods are not available in a shim, but they are supported by IE8+.)

Number.prototype.toFixed

Object.keys

String.prototype.trim

I list this next set separately as they are very important. There are often referred to as the functional programming additions, but regardless of how you feel about functional programming, you need to learn to use these.

For example, JSPerf.com, shows the filter method performing 6x faster than loop-based code, map performing 5x faster, indexOf performing 30x faster and forEach performing an amazing 46x faster. (Results vary with what you do inside the callback — but it seems to always be a good idea to avoid the loop when you can.) This is true because the internal Array.prototype.method implementations can avoid much of JavaScript’s dynamic overhead inside these methods.

Array.prototype.every
Array.prototype.filter
Array.prototype.forEach
Array.prototype.indexOf
Array.prototype.lastIndexOf
Array.prototype.map
Array.prototype.some
Array.prototype.reduce
Array.prototype.reduceRight
Function.prototype.bind

Function.prototype.bind is an exception here, it doesn’t shim as well as the rest, so you’ll need to be careful when using it. I include it as it completes the Function Programming additions in ES5. Unlike the array methods, it doesn’t give additional performance. It is used to make a function that executes as a method bound to an object and for functional programming techniques (partial application, currying).