Additionally, I understand that (new Foobar()).test !== (new
Foobar()).test, so that these functions are actually created unique to
each instance, but if you add a parameter to initialize 'abc' to a
number, a differently valued SMI on the object doesn't change the
hidden class, so why does a different function? They're both Function
objects, so that seems weird.
A potentially related question is why functions are treated so weirdly
in objects depending on how they're added. For instance:
var z = {test: function () {}};
z.test2 = function () {};
var i;
console.time('test speed');
for (i = 0; i < 10000000; i++) {
z.test();
}
console.timeEnd('test speed');
console.time('test2 speed');
for (i = 0; i < 10000000; i++) {
z.test2();
}
console.timeEnd('test2 speed');
Result:
test speed: 99ms
test2 speed: 21ms
- Justin
On Jul 18, 6:04 pm, jMerliN <[email protected]> wrote:
> So I can't get my head around why this happens (I haven't dug through
> v8's code to try to figure it out either), but this is really
> inconsistent to me with how v8 constructs hidden classes in general.
> The following is running in Node.js v0.8.2 (V8 v3.11.10.12).
>
> Here's the code:http://pastebin.com/2gKWrfHp
>
> Here's the output, and the deopt trace:http://pastebin.com/WerQuGLZ
>
> Calling Foo.prototype.runTest with any Foo object results in similar
> performance (unless you change the hidden class, as expected). Bar
> expectedly deoptimizes because abc is stored on the proto and isn't
> actually on the constructed object until the first call, causing the
> optimized function (once it gets hot, which is after the object has
> changed hidden class) to bailout on the next attempt with a new Bar
> object.
>
> It gets weird with Foobar. test is added directly to the object, the
> only difference is that this is a function, not a primitive, but it
> seems like the hidden classes of objects from Foobar's constructor
> should be the same. The first run is performant, equivalent to Foo
> (expected). Though running the test again with a new Foobar
> deoptimizes it. I can't at all understand why.
>
> Thanks,
> Justin
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users