0

As is known, Internet Explorer (at least <= 8) does not support several javascript Array functions (indexOf, filter, etc). This stackoverflow answer provides some decent implementations of most of these missing Array functions.

My question: is it preferable to provide implementations like those in the answer for your website or simply rely on an external library (jQuery, etc)?

Is there a best practice? I'm looking for insight from those with more practical experience than I with this situation.

The lack of dependency on an external library is always a plus (although they're largely ubiquitous in most cases). But there's no guarantee that any custom implementation will cover ALL edge cases and it introduces something to maintain.

Community
  • 1
  • 1
kdawg
  • 2,019
  • 21
  • 31
  • 1
    It's always good to have the Array methods around... they're helpful regardless of whether you use a library or not. Just stick `` in the header. See, it's not that hard `:)` – Šime Vidas Jun 15 '12 at 20:41
  • didn't even know es5-shim existed! thanks! – kdawg Jun 15 '12 at 20:43
  • Whenever a feature *can* be implemented manually with JavaScript, you can be sure that there's a shim for it. – Šime Vidas Jun 15 '12 at 20:45

2 Answers2

1

best practice is probably using the es5-shim which lets you use javascript in all browsers as if it was fully ECMAScript 5 compatible (with a few limitations, for example the second param of Object.create can't be emulated).

This has the sheer advantage that it only acts on browsers that lack such extensions, so modern browsers will use the built-in optimized versions of the code.

Also, you avoid leveraging on an external library that defines their own spec, which in case of jQuery is kinda broken like $.each having the params inverted from what the spec says for Array.prototype.forEach, or weird naming conventions like $.proxy for what Function.prototype.bind does.

gonchuki
  • 4,064
  • 22
  • 33
0

What is and isn't preferable is hard for others to answer, but here's what I would prefer:

If it's only a handful of functions, then I would probably just implement them myself. However, like you say, you might not be able to consider every edge case (at least not immediately) and you add more stuff that needs to be maintained. Popular libraries (like Underscore.js which would be useful in your case) are made and maintained by many people (usually), and their main focus is the functionality and performance of that library. So it's much more likely they'll be able to find, and fix, any issues a lot faster than you can. It's also likely you'll find more and more things the library can do for you as your project progresses. There's a good chance there are cross-browser issues they've thought of and taken into account that you wouldn't have noticed until far out into testing, or even worse, a customer complained.

Getting to know a popular library can be a real time saver on future projects too. There's also the benefit of having more confidence in a good library, both for you and/or any employers/clients.

That being said - it's a trade-off, because most libraries include more functionality than you'll end up using, so there's wasted space causing more bandwidth to be consumed and more memory used than necessary. The bandwidth thing is mostly solved with proper caching.

Also, there are a TON of great libraries out there, for just about anything - so you can easily end up having to include many 100's of kilobytes, or even more, of Javascript if you go bananas and use too many libraries.

So to sum it up: I would prefer to outsource the creation and maintaining of utility functions to those who want to do that stuff for free, and rather spend my time and energy on the things that more directly create value for the end-user.

Joe Dyndale
  • 1,073
  • 1
  • 15
  • 32