As3 {} against the new object

What are the differences, pros or cons

var obj = {};

VS

var obj = new Object();

All I know is that the second example takes longer. Is there any real benefit?

** EDIT **

function loop() {
    var start = (new Date()).getTime();
    for(var i = 0; i < 1000000; ++i) {
        //var b = {}; // takes ~548ms on my machine
        var b = new Object(); // takes ~287ms on my machine
    }
    trace((new Date()).getTime() - start);
    setTimeout(loop, 1);
}
loop();

If you switch between var b = {};and var b = new Object();, you will see differences in performance. They are the opposite of my recollection and what I mentioned in the question.

+5
source share
2 answers

As far as I know, they are equivalent. β€œThe second example takes longer,” I suppose that you mean just in time to enter an operator and perhaps an immeasurable amount of time to evaluate, but the execution time should be equivalent.

+3
source

, new Object() - , {}, , , , .

:

function time(amount:int, test:Function):Number
{
    var average:Number = 0;
    var averages:Array = [];

    for(var n:int = 0; n<amount; n++)
    {
        var start:Number = getTimer();

        test();

        averages[averages.length] = getTimer() - start;
    }

    for each(var a:Number in averages) average += a;

    return average / averages.length;
}

:

function short():void
{
    for(var i:int = 0; i<1000000; i++) var obj:Object = {};
}

function long():void
{
    for(var i:int = 0; i<1000000; i++) var obj:Object = new Object();
}

:

trace(time(5, short));  // 278.6
trace(time(5, long));   // 153.6

45% new Object(), , ( 1 000 000 ).

{} .

+3

All Articles