DEV Community

Brian Neville-O'Neill
Brian Neville-O'Neill

Posted on • Originally published at blog.logrocket.com on

Comparison of the top Array verification utilities on npm

Source: http://www.monkeyuser.com/2017/npm-delivery/

A few weeks ago, I was working on my side project, array-length, a highly performant new JavaScript library for calculating the number of elements in a JavaScript array, when I faced an interesting challenge. I needed a way of validating that a users’ input was truly an array, and not some other type of object that might break my algorithm’s complex control flow.

As is often the case with any sufficiently lucrative business opportunity, there are a variety of solutions on the market that aim to solve this problem. They range from focused, best-of-breed libraries ideal for startups and mid-market customers, to highly scalable, enterprise-grade solutions that offer a wide range of functionality.

Given the myriad of options, I was surprised to find that little literature exists to help a buyer make an informed decision. Perhaps the array verification market is too new to be covered by the likes of Gartner and Forrester, my typical sources for reviews on JavaScript libraries.

Instead, I decided to spend a few weeks surveying the landscape and writing up my findings. Below you’ll find my summary of the top three solutions across a variety of metrics including performance, scalability, maintainability, interoperability, debuggability, readability, and web-scaleability.

LogRocket Free Trial Banner

isarray

isarray is an excellent npm package that can accurately detect whether its given argument is or is not an array. While the README states that, “isarray is for older browsers and deprecated Node.js versions.” I found that it works quite well with new browsers and the latest version of Node.

It take a single argument and returns a boolean of whether or not the given object is indeed an Array.

Usage

var isArray = require('isarray');
console.log(isArray([])); // => true
console.log(isArray({})); // => false
Enter fullscreen mode Exit fullscreen mode

Pros

  • concise, highly-expressive syntax
  • works well with MongoDB
  • Excellent ROI (only 2 month pay back period)

Cons

  • No 3rd party plugin ecosystem
  • 3 open GitHub issues

is-array

is-array takes a wildly different approach to JavaScript array verification, opting for a hyphen instead of an empty string between is and array in the package title. It offers significantly improved readability over the compound title of isarray while still providing the majority of the functionality expected by the discerning engineer.

It take a single argument and returns a boolean of whether or not the given object is indeed an Array.

Usage

var isArray = require('is-array');
console.log(isArray([])); // => true
console.log(isArray({})); // => false
Enter fullscreen mode Exit fullscreen mode

Pros

  • It’s cool because it’s less popular

Cons

  • longer name means larger source code (slower Git pushes, less free hard drive space, etc)
  • Permissive MIT license means a large company could Embrace, Extend and Extinguish it

Array.isArray()

A last resort, suitable only in cases when 3rd party modules are not allowed (enterprise security constraints, use on a dessert island, etc), Array.isArray() is a built-in JavaScript method that was introduced along with the ES5 standard.

It take a single argument and returns a boolean of whether or not the given object is indeed an Array.

Usage

console.log(Array.isArray([])); // => true
console.log(Array.isArray({})); // => false
Enter fullscreen mode Exit fullscreen mode

Pros

  • At least 95% accurate

Cons

  • Not on npm
  • Not supported in Opera 4
  • Will throw an error if you try to import it with webpack

References

Rog Locket is a developer and humorist who writes a weekly satirical column for the LogRocket blog. He is entirely fictional.


Plug: LogRocket, a DVR for web apps

LogRocket is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single page apps.

Try it for free.


Top comments (3)

Collapse
 
tyrw profile image
Tyler Warnock

Is there a reason you didn't include lodash isArray? lodash.com/docs#isArray

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Because it's not a serious comparison. Brian's point is: Why the hell do you need a library to find out if something is an array in JS?!

Collapse
 
ayoalfonso profile image
AyoAlfonso

Was going to ask the same thing.