Closed
Bug 782906
Opened 13 years ago
Closed 1 year ago
Improve performance of Object.create
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
RESOLVED
INCOMPLETE
People
(Reporter: kael, Unassigned)
References
Details
(Whiteboard: [js:3][Shumway])
Right now, Object.create is dramatically slower than calling new on an equivalent empty constructor. It's fine that constructors get optimized more than if you were to call create and then set properties, but the fact that create is so suboptimal performance-wise means that it's actually faster to use a JS polyfill in many cases.
To give a concrete example, JSIL has a runtime function that implements cloning for structs. I used to do it by calling Object.create on the prototype for the struct, then copying member values from the instance being cloned to the new, empty instance. Profiling this in FF showed a large amount of CPU time just being spent executing Object.create (it looks like it relies on a stub?). Replacing it with the (relatively nasty) polyfill:
var copyConstructor = function (source) {
// copy the members
this.foo = source.foo;
...
};
copyConstructor.prototype = theType.prototype;
Dramatically improved performance. I don't know how common use of Object.create actually is in the wild, but it would at least be nice if it was closer to the perf of the polyfill.
| Reporter | ||
Comment 1•13 years ago
|
||
Oh, I forgot:
Part of why this matters is that in the general case, the polyfill will be worse. This is because the polyfill can't be fully general without creating an entirely new constructor function (and setting its prototype) every time you create an instance, unless it does some sort of insane caching shenanigans. So it should be pretty trivial to beat the polyfill in the general case.
Comment 2•13 years ago
|
||
IIRC, v8 is also tremendously slow on Object.create?
| Reporter | ||
Comment 3•13 years ago
|
||
Based on http://jsperf.com/new-vs-object-create/6 I believe they are, yes. I'm not sure if their reason for it is the same, though.
Updated•13 years ago
|
Whiteboard: [js:3]
Comment 4•12 years ago
|
||
Not only are we faster than Chrome (27, ~6x) and Safari (6.0.5, ~1.1x), we are, weirdly, also more than twice as fast at the `Object.create` test as on the `new` test.
While I don't know why that is and we should obviously work on making the `new` test faster, I'm going to declare victory here.
Status: NEW → RESOLVED
Closed: 12 years ago
Resolution: --- → FIXED
| Reporter | ||
Comment 5•12 years ago
|
||
Object.create isn't faster in FF, that's just jsperf being terrible and getting entirely optimized out.
http://jsperf.com/new-vs-object-create/12
I modified the test to make it harder to optimize out and now 'new' is faster just like before (unless I made a mistake). I just recently replaced an 'Object.create' in a JSIL library with 'new' and saw a speed improvement in FF, so I'm pretty sure this isn't actually fixed.
Comment 6•12 years ago
|
||
Urgh. So, if it gets optimized out, how come we're still an order of a magnitude slower than the competition is with the `new Klass()` case?
Anyway, with today's Nightly, with your modified test the `Object.create()` version is still 50% faster for me.
I'm pretty much done with jsperf at this point, so prepare for gistbench[1], with sandboxed srcdoc'd iframes[2] to create low-overhead, predictable benchmarks.
[1]: working title
[2]: with rawgithub.com-fallback for browsers without srcdoc support. Such as us before the Nightly released later today
Status: RESOLVED → REOPENED
Resolution: FIXED → ---
Updated•11 years ago
|
Whiteboard: [js:3] → [js:3][Shumway]
| Assignee | ||
Updated•11 years ago
|
Assignee: general → nobody
Updated•10 years ago
|
Blocks: shumway-later
Updated•3 years ago
|
Severity: normal → S3
Comment 7•1 year ago
|
||
Closing this bug as anything shuway or JSIL is not a priority these days. And code like this should target WASM these days.
Status: REOPENED → RESOLVED
Closed: 12 years ago → 1 year ago
Resolution: --- → INCOMPLETE
You need to log in
before you can comment on or make changes to this bug.
Description
•