Hi, As we mentioned during the investigation of "with" statement and scopes in V8 we have created some JS example to experiment with the usage of "with".
These examples do the same thing but have different performance:
1. This is the best performance that we want to reach with "with"
statement:
for(var i = 0; i < 1000000; ++i) Math.sin(i);
time: 59msec
2. A general use-case of the previous example with "with" statement:
for(var i = 0; i < 1000000; ++i) with(o) Math.sin(i);
time: 465msec
3. It's much slower when "for" is inside "with":
with(o) { for(var i = 0; i < 1000000; ++i) Math.sin(i); }
time: 960msec
4. We can reach better performance with "with" if we use function inside
"with":
with(o) (function() { for(var i = 0; i < 1000000; ++i) Math.sin(i); })()
time: 221msec
The fourth example just a suggestion to using "with" statement and reach
better performace without modify the JS engine.
Would it be possible to use the latest form for your purposes?
Regards,
Szeged Team
P.S.: I have also attached the benchmark JS file so you can give a
try ;)
On Thu, 2011-04-21 at 19:03 +0200, Zoltan Herczeg wrote:
> > for (var i = 0; i < 1000000; ++i) with (o) Math.sin(i);
> > with (o) for (var i = 0; i < 1000000; ++i) Math.sin(i);
> > for (var i = 0; i < 1000000; ++i) Math.sin(i);
>
> The second one is really heavy, since it resolves both 'i' and 'Math' in
> every iteration. And resolving is expensive, even on the normal global
> scope.
>
> Regards,
> Zoltan
>
>
> _______________________________________________
> Qt-script mailing list
> [email protected]
> http://lists.qt.nokia.com/mailman/listinfo/qt-script
with.js
Description: application/javascript
_______________________________________________ Qt-script mailing list [email protected] http://lists.qt.nokia.com/mailman/listinfo/qt-script
