Archives Posts
‘with’ or without?
September 6th, 2007 by kangax
I remember back in the days there were these huge articles about refactoring Javascript to perform faster. “with” statements were considered a huge slow down…
Just to satisfy my curiosity I rolled a quick test:
Foo = { Bar: { Baz: { Qux: { Quux: function(){} } } } } // Plain call console.time('test'); for (var i=0; i<100000; ++i) { Foo.Bar.Baz.Qux.Quux(); } console.timeEnd('test'); // 562 ms // "with" outside the loop console.time('test'); with(Foo.Bar.Baz.Qux) { for (var i=0; i<100000; ++i) { Quux(); } } console.timeEnd('test'); // 359 ms // with inside the loop console.time('test'); for (var i=0; i<100000; ++i) { with(Foo.Bar.Baz.Qux) { Quux(); } } console.timeEnd('test'); // 1891 ms // aliasing console.time('test'); for (var i=0, q=Foo.Bar.Baz.Qux.Quux; i<100000; ++i) { q(); } console.timeEnd('test'); // 422 ms