jQuery.extend / jQuery.fn.extend triggers slow path in V8 (and it looks like easy to fix) #4245
Labels
Milestone
Comments
marjakh
added a commit
to marjakh/jquery
that referenced
this issue
Nov 29, 2018
Read target[name] only when it's needed. In addition to doing the property read only when needed, this avoids a slow path in V8 (see issue for more details). Fixes jquery#4245
marjakh
added a commit
to marjakh/jquery
that referenced
this issue
Nov 30, 2018
Read target[name] only when it's needed. In addition to doing the property read only when needed, this avoids a slow path in V8 (see issue for more details). Fixes jquery#4245
mgol
added a commit
that referenced
this issue
Dec 12, 2018
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Description
In V8, adding properties into the prototype chain, interleaved with property access, results in copying fast mode maps repeatedly. (A "map" describes the internal layout of an object.)
Example:
function Foo() { ... }
// Add properties (prototype in setup stage)
Foo.prototype.a = function() { ... }
Foo.prototype.b = function() { ... }
let f = new Foo();
for (let i = 0; i < 100; ++i) {
f.a; // Second access from the same code location turns prototype into fast mode
Foo.prototype.a; // This kind of access triggers the behavior too
}
// Add more properties (prototype stays in fast mode; map copied)
Foo.prototype.c = function() { ... }
// Map copied again
Foo.prototype.d = function() { ... }
jQuery init phase also has this access pattern:
This is a lot of V8 internals (also fixing this on V8 side is on our radar), but looks like the jQuery side fix is simple and also makes sense software-engineering-wise: move "src = target[ name ];" into the branch where it's used. When calling extend with an object which contains functions, we don't go into that branch.
Link to test case
This is not a functional change.
The text was updated successfully, but these errors were encountered: