Test object length property

JavaScript performance comparison

Test case created by Josh Bush


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

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></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;

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');
var a = typeComparison(array);
var b = typeComparison(objectWithoutLengthProperty);
var c = typeComparison(objectWithNonNumericLengthProperty);
if (!a || b || c)
throw new Error('broken comparison');
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');

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

Compare results of other browsers


Comment form temporarily disabled.

Add a comment