Question

I have both es5-shim.js and underscore.js in my JavaScript project.

es5-shim.js just add some javascript native functions like reduce and some on arrays for Internet Explorer and some old browsers. underscore.js add the same thing (but with a different syntax) and many more thing (utility functions on objects and arrays).

But, if the functions added by es5-shim exists, underscore.js use them.

So, on a "recent" browser like Firefox or Chrome, underscore.js use the native functions of the browser. I think it's better than a pure javascript function. But on Internet Explorer, underscore.js use the es5-shim functions. If I remove es5-shim.js, underscore.js use its own functions.

Any advice on that ? Should i remove es5-shim from my project and only use underscore.js ? Or should i let underscore.js use es5-shim's functions ?

Was it helpful?

Solution 2

es5-shim, as you rightly mentioned, adds some JavaScript functions like map, reduce, some and forEach on Array if the browser's JavaScript engine is not ES5 compatible. Underscore.js adds utility methods similar to the above (and more) within the "_" namespace. So, in this case, using both in the same application is redundant.

That said, es5-shim, adds a few more functions like Date.now, Date.toJSON, Function.bind, String.trim (and more) some which don't have direct equivalents in Underscore.js. For example Underscore does provide _.bind and _.bindAll that is equivalent to Function.bind, but Underscore.js does not provide Date.now and Date.toJSON.

So, if you use these additional methods provided by es5-shim you can stay with both es5-shim and Underscore.js in your application. But, if you are not using these additional methods, then using es5-shim would just be an unnecessary increase in application size and waste of bandwidth.

OTHER TIPS

I disagree with @ganeshi's answer.

If your primary use of Underscore is things that already come with ES5 like reduce() and some() then I suggest you remove underscore.js.

If you code to ES5 methods now, you can simply remove ES5-shim when you no longer support IE8 or Firefox 3.

If you use underscore now, then when you no longer support IE8 or Firefox 3 you still will carry an extra copy of a large amount of things most browsers already provide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top