How about wrapping the fragments in anonymous closures? Roughly:

script_to_execute = "(function() {" + fragment + "})();";

That should get you most of what you want, except for undoing
monkey-patching of Array.prototype and stuff, but maybe that's good enough,
and it's certainly easy and fast.


On Wed, Jul 16, 2014 at 8:35 AM, Oliver Bock <oli...@g7.org> wrote:

> What is the most efficient way to clean up the global object between
> running fragments of JavaScript?
>
> I am running a fragment of JavaScript in a tight loop to make a simple
> calculation for each of hundreds of thousands of inputs.  I don't want
> variables set by one execution to bleed into the next.
>
> My first thought was to try to swap in a different global object for each
> v8 iteration, but I can find no mechanism to do that.  (Earlier versions of
> v8 might have supported it via something like Context.ReattachGlobal()
> <https://groups.google.com/forum/#!msg/v8-users/FfqgU3jEKf4>, but that
> function no longer exists.)
>
> I can manually iterate over the global object and clean up after myself
> (as this guy does
> <https://groups.google.com/forum/#%21topic/v8-users/q2CDsy58Dcc>), but my
> JavaScript fragments are fairly simple and the cleanup dominates execution
> time (because v8 is so fast).
>
> I think recreating the Context each time will be even slower than this.
> particularly because I will need to repopulate it with various objects.
>
>
>   Oliver
>
> --
> --
> v8-users mailing list
> v8-users@googlegroups.com
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to v8-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to