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?
- oneAccording 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
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
withwas used outside the cycle , the results were3725msversus3312ms. - In case the
withconstruction was inside the loop at each iteration , the results are3915msversus3313ms.
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
|