Test object length property

JavaScript performance comparison

Test case created by Josh Bush

Info

Testing underscore's .each() method of length property detection vs jQuery's.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var array = [1,2,3],
        objectWithoutLengthProperty = {},
        objectWithNonNumericLengthProperty = {length:"72 feet"};
   
    function unaryPlusComparison(obj){
       var len=obj.length;
       return len === +len;
    }
    function unaryPlusComparison2(obj){
       return +obj.length >=0;
    }
    function typeComparison(obj){
       var len=obj.length;
       return typeof len === "number" && len >= 0;
    }
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Unary + Operator
var a = unaryPlusComparison(array);
var b = unaryPlusComparison(objectWithoutLengthProperty);
var c = unaryPlusComparison(objectWithNonNumericLengthProperty);
if (!a || b || c)
throw new Error('broken comparison');
pending…
typeComparison
var a = typeComparison(array);
var b = typeComparison(objectWithoutLengthProperty);
var c = typeComparison(objectWithNonNumericLengthProperty);
if (!a || b || c)
throw new Error('broken comparison');
pending…
Unary + Operator without equality
var a = unaryPlusComparison2(array);
var b = unaryPlusComparison2(objectWithoutLengthProperty);
var c = unaryPlusComparison2(objectWithNonNumericLengthProperty);
if (!a || b || c)
throw new Error('broken comparison');
pending…

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

Compare results of other browsers

0 comments

Add a comment