Checking Performance with jsperf – core of a sustainability test


I’ve been looking closely at the site JSPerf, which posts benchmarks for common programming operation, and comparisions of various browsers implementing the operations. A great feature – is the ablity to add alternative ways of performing the same operation. The different methods then run in different browsers, and the results are tabulated.

http://jsperf.com/

As an example, in JavaScript, since strong typing is missing (like AS2 and unlike AS3) we sometimes don’t know if an object we’re working with is a function, array, or simple object. As with many things in JavaScript’s 20-year evolution, there are differences between different browsers and historical versions of browsers. So, there may be many ways of implementing a test. Lots of discussion boards go back and forth about the best cross-browser solutions.

As an example, here’s the results from testing different methods of figuring out whether a JS object is actually an array:

http://jsperf.com/array-type-checking/4

With my less than hardcorde JS experience, I would have done something like:

if(typeof a.length !== 'undefined') { //non-array objects won't have a .length property
//not an array
}

But this turns out to be the SLOWEST of the various methods. The alternate methods are many, many times faster.

What’s great about JSperf is that we are getting numbers, rather than discussion. If you do a Google search on array type checking in javascript, you will see many solutions argued over in discussion forums. Many are proposed without testing, and frequently another poster says the given example “doesn’t work” – but gives no counter-method. All in all, it takes a long time to find the most efficient method, and the best discussion postings typically just cite a method from a standard library like JQuery or Yahoo! YUI, even attacking others for not using the example. But a quick check of JSperf solutions versus those in common libraries shows that many aren’t implementing an optimum solution.

The results of JSPerf point in the direction of a “green ingredient” database for JS coding. Probably, some of the stuff on JSFiddle and similar sites could be used in the same way.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s