Short int check vs trad int check

Benchmark created by Maarten Jacobs on


Description

In the underscore.js library, specifically the very first function called forEach (alias for each), a test is made to check if the length property of the given object is an integer. The way it does this is not the traditional typeof check, but by using the plus operator

Preparation HTML

<script>
  var foo = [];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Traditional number check
if(typeof foo.length === 'number'){
console.log('It be a number, Batman!');
}
ready
Plus operator number check
if(foo.length === +foo.length){
console.log('It be a number, Batman!');
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Maarten Jacobs on