Probably an easy question for anyone who's gotten into the internals
of V8 to answer: Can/Does V8 use the same function code for multiple
Function objects?
For example, consider:
* * * *
// Constructor function creating Thingy objects
function Thing() {
var privateData, morePrivateData;
this.doSomething = doSomething;
function doSomething() {
// Lots of code here, presumably using privateData and
morePrivateData
}
}
* * * *
Now suppose I create 1,000 `Thingy` objects via `new Thingy()` 1,000
times. Naturally, that will involve creating 1,000 `doSomething`
Function objects (you never know, I might do `this.doSomething.foo =
"bar";`, so the Function objects *must* be different), each with its
link back to the execution context in which it was created, etc.
But does V8 reuse the *code* of the `doSomething` function across
those 1,000 Function objects? Just setting up the context before
jumping into it? Or is all the code related to `doSomething`
duplicated in memory 1,000 times? (Which would seem un-V8-like. :-) )
I could try to establish this empirically, by creating a bunch of
`Thingy` objects with big `doSomething` functions and seeing how much
memory is used vs. creating a bunch of `Thingy` objects with very
small `doSomething` functions, but I'd prefer not to infer it if
someone happens to know off-hand.
Thanks in advance,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com
(Sorry for the late reply, I asked GGroups to notify me of replies,
and it still shows that it will, it just...didn't. Odd.)
Best,
-- T.J.
But good to know about V8.
Cheers,
-- T.J.