javascript - speed of setter functions not what expected -



javascript - speed of setter functions not what expected -

i comparing performance of 2 different types of setters shown below:

var q={}; var z={}; (function(){ var x=1; q.x=function(){ homecoming x; }; q.x.set=function(val){ x=val; }; })(); (function(){ var x=1; z.x=function(){ homecoming x; }; z.x_set=function(val){ x=val; }; })();

the difference lies in setter stored: q.x.set vs z.x_set. expected z variant run faster since requires 1 less table look-up. on contrary q variant consistently faster. ideas why? here jsperf: http://jsperf.com/hq7f3

your experiment has few results various browsers. lets @ results.

so expect z faster q. when examine results see 2 different categories of result.

results nearly identical, trending towards q beingness faster. or z faster.

what tells me @ mercy of implementation details of various js engines. different js engines utilize ton of low level optimizations alter timing characteristics of these little atomic operations. perhaps q pattern triggers optimization z not in browsers. or perhaps browsers have optimization z pattern older browsers did not support. given lack of consistency, why hard answer.

one thing clear form results far, on average z faster q. q faster, when z faster, it's lot faster.

but practically, difference negligible. tiny operation. tiny changes in tiny operation can lead big changes form percentage perspective, very little changes performance profile in larger application. personally, think academically interesting, of little actual consequence.

javascript getter-setter jsperf

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -