-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(core): Use native bind function instead of own (fix #7408) #7491
Conversation
Simple refactor bind function for easy revert. Custom bind implementation performance is lower than native. https://jsperf.com/vue-bind-perf
Some browsers in the support range e.g. PhantomJS don't have |
Maybe it's better to decide at first module run which implementation to use? e.g.: export const bind = Function.prototype.bind ? nativeBind : bind |
const ownBind = (fn: Function, ctx: Object) => {
function boundFn(a) {
const l = arguments.length
return l
? l > 1
? fn.apply(ctx, arguments)
: fn.call(ctx, a)
: fn.call(ctx)
}
boundFn._length = fn.length
return boundFn
}
export const bind = Function.prototype.bind ? Function.prototype.bind : ownBind |
Why we should carry polyfill for this? Phantomjs is a We increase bundle size for every build for ^ case? I just didn't understood. |
I think
Should be: function ownBind(fn: function, ctx: object) {
// internal/current implementation ...
}
function nativeBind(fn: function, ctx: object) {
return fn.bind(ctx)
}
export const bind = Function.prototype.bind ? nativeBind : ownBind Because of how native |
So maybe wrap polyfill version in I don't see its as as breaking change. |
It is a breaking change if you remove a polyfill. Someone's tests that were able to run in PhantomJS would break suddenly in a patch release because of that. |
Then its ready for merge. |
I think @OEvgeny 's suggestion should be applied so that we only check native bind once on initialization. |
src/shared/util.js
Outdated
boundFn._length = fn.length | ||
return boundFn | ||
} | ||
|
||
function nativeBind(fn: Function, ctx: Object): Function { | ||
return fn.bind(ctx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's missing the optional arguments, cause different behavior to ownBind
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see no optional arguments in ownBind
. Are you about fn._length
? If yes. It shouldn't be a problem because we should check both length
and _length
since we don't know exactly was the fn
bound via our ownBind
or not. e.g.:
(enterHook._length || enterHook.length) > 1 |
I think this broke something. if fn.bind is undefined it blows up. |
@rlightner please provide a reproduction. |
EDIT: I think this issue exposed a coding error. This should be a computed and not a method. It worked somehow before this fix though. I'll try. The fn being passed in is a method in a template that contains:
and it's being passed in to |
As @rlightner pointed out, this exposed a coding error if whatever's provided to the Version 2.5.13 https://jsfiddle.net/ujxu9mvd/ Renders fine, fails silently |
Simple refactor bind function for easy revert.
Custom bind implementation performance is lower than native. ( https://jsperf.com/vue-bind-perf )
issue link: #7408
What kind of change does this PR introduce? (check at least one)
Does this PR introduce a breaking change? (check one)
If yes, please describe the impact and migration path for existing applications:
The PR fulfills these requirements:
dev
branch for v2.x (or to a previous version branch), not themaster
branchfix #xxx[,#xxx]
, where "xxx" is the issue number)If adding a new feature, the PR's description includes:
Other information: