Actually I read a long time ago about what works slowly, in practice I did not use it, but I began to read another javascript book and remembered ...
Are you really interested in abandoning this design despite all its conveniences?

  • one
    According to ECMAScript-5, in Strict mode, use with is prohibited. - timka_s
  • I know. There are a lot of things forbidden there, and I use it once in 100 years. In ECMAScript-5, I will restore it when it is supported by> 80% of browsers, but for now, this is just a recommendation. Yes, and Strict mode is also, in fact, just a recommendation - Zowie

1 answer 1

There is a benchmark design with in js - http://www.dynamicsitesolutions.com/demos/jswithspeed_test.html

 function withTest() { var temp = ''; with(document.forms[0]) { for(var i=0;i<100000;i++) { temp = ''; temp = id; temp = ''; temp = action; temp = ''; temp = innerHTML; } } } function alternativeMethodTest() { var temp = ''; var f = document.forms[0]; for(var i=0;i<100000;i++) { temp = ''; temp = f.id; temp = ''; temp = f.action; temp = ''; temp = f.innerHTML; } } 

In my case, the difference was completely insignificant.

  • If the construction with was used outside the cycle , the results were 3725ms versus 3312ms .
  • In case the with construction was inside the loop at each iteration , the results are 3915ms versus 3313ms .

Ie, if you speculate, then it is certainly worthwhile to apply, but if, for example, you want to optimize any code snippet, then with can be abandoned to improve performance.

PS

Chrome 14.0.835.163

I admit that in other browsers the result may be different, although I do not believe that it will be catastrophically different.

  • thanks for the test, the most unreal lazy was to do it ... <br> Indeed, the difference is insignificant ... - Zowie