Array by literals vs constructors

JavaScript performance comparison

Test case created by Doug Gale

Preparation code

<script>
var columns;

function ExprAlias(expr, alias) {
  this.expr = expr;
  this.alias = alias;
}

function addColumn(expr, alias) {
    if (columns !== undefined)
        columns.push({
            expr: expr,
            alias: alias
        });
    else
        columns = [{
            expr: expr,
            alias: alias
        }];
}

function addColumn2(expr, alias) {
    if (columns !== undefined)
        columns.push(new ExprAlias(expr, alias));
    else
        columns = [new ExprAlias(expr, alias)];
}

</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
literal
for (var i = 0; i < 20; ++i)
    addColumn('a', 'b');
columns = undefined;
 
pending…
constructor
for (var i = 0; i < 20; ++i)
    addColumn2('a', 'b');
columns = undefined;
 
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