From 8c23f992309eda75c0b914d9da6660a117cd393d Mon Sep 17 00:00:00 2001 From: dmitry-vakhnenko <33278950+dmitry-vakhnenko@users.noreply.github.com> Date: Sun, 21 Jan 2018 00:59:16 +0300 Subject: [PATCH 1/5] Use native bind Simple refactor bind function for easy revert. Custom bind implementation performance is lower than native. https://jsperf.com/vue-bind-perf --- src/shared/util.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/shared/util.js b/src/shared/util.js index a0bc957ee93..1cea2897e0a 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -174,20 +174,10 @@ export const hyphenate = cached((str: string): string => { }) /** - * Simple bind, faster than native + * Bind funciton context */ export function bind (fn: Function, ctx: Object): Function { - function boundFn (a) { - const l: number = arguments.length - return l - ? l > 1 - ? fn.apply(ctx, arguments) - : fn.call(ctx, a) - : fn.call(ctx) - } - // record original fn length - boundFn._length = fn.length - return boundFn + return fn.bind(ctx) } /** From 46965bed990888f0d6ea11e14c202d4c05f10ad3 Mon Sep 17 00:00:00 2001 From: dmitry-vakhnenko <33278950+dmitry-vakhnenko@users.noreply.github.com> Date: Sun, 21 Jan 2018 02:23:15 +0300 Subject: [PATCH 2/5] use bind polyfill if Function.bind not exits --- src/shared/util.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/shared/util.js b/src/shared/util.js index 1cea2897e0a..508eee2185e 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -177,6 +177,21 @@ export const hyphenate = cached((str: string): string => { * Bind funciton context */ export function bind (fn: Function, ctx: Object): Function { + if(!Function.prototype.bind) { + function boundFn (a) { + const l = arguments.length + return l + ? l > 1 + ? fn.apply(ctx, arguments) + : fn.call(ctx, a) + : fn.call(ctx) + } + // record original fn length + boundFn._length = fn.length + + return boundFn + } + return fn.bind(ctx) } From 454a201f9961b13115921c882ea00c4f98ea9805 Mon Sep 17 00:00:00 2001 From: dmitry-vakhnenko <33278950+dmitry-vakhnenko@users.noreply.github.com> Date: Sun, 21 Jan 2018 02:25:18 +0300 Subject: [PATCH 3/5] Update util.js --- src/shared/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/util.js b/src/shared/util.js index 508eee2185e..2ade6583308 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -174,7 +174,7 @@ export const hyphenate = cached((str: string): string => { }) /** - * Bind funciton context + * Bind function context */ export function bind (fn: Function, ctx: Object): Function { if(!Function.prototype.bind) { From 2a48f51a6bd1302557dcd50bad896ca6591af328 Mon Sep 17 00:00:00 2001 From: dmitry-vakhnenko <33278950+dmitry-vakhnenko@users.noreply.github.com> Date: Mon, 22 Jan 2018 18:00:21 +0300 Subject: [PATCH 4/5] Update util.js --- src/shared/util.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/shared/util.js b/src/shared/util.js index 2ade6583308..e62c166ef40 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -176,25 +176,26 @@ export const hyphenate = cached((str: string): string => { /** * Bind function context */ -export function bind (fn: Function, ctx: Object): Function { - if(!Function.prototype.bind) { - function boundFn (a) { - const l = arguments.length - return l - ? l > 1 - ? fn.apply(ctx, arguments) - : fn.call(ctx, a) - : fn.call(ctx) - } - // record original fn length - boundFn._length = fn.length - - return boundFn +function ownBind (fn: Function, ctx: Object): Function { + 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 +} + +function nativeBind(fn: Function, ctx: Object): Function { return fn.bind(ctx) } +export const bind = Function.prototype.bind ? nativeBind : ownBind + /** * Convert an Array-like object to a real Array. */ From 368c5f9caf493d0657e863d24ab3049e454bfb1e Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 8 Mar 2018 10:07:13 -0500 Subject: [PATCH 5/5] Update util.js --- src/shared/util.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/shared/util.js b/src/shared/util.js index e62c166ef40..3a7664b76e9 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -174,9 +174,13 @@ export const hyphenate = cached((str: string): string => { }) /** - * Bind function context + * Simple bind polyfill for environments that do not support it... e.g. + * PhantomJS 1.x. Technically we don't need this anymore since native bind is + * now more performant in most browsers, but removing it would be breaking for + * code that was able to run in PhantomJS 1.x, so this must be kept for + * backwards compatibility. */ -function ownBind (fn: Function, ctx: Object): Function { +function polyfillBind (fn: Function, ctx: Object): Function { function boundFn (a) { const l = arguments.length return l @@ -190,11 +194,13 @@ function ownBind (fn: Function, ctx: Object): Function { return boundFn } -function nativeBind(fn: Function, ctx: Object): Function { +function nativeBind (fn: Function, ctx: Object): Function { return fn.bind(ctx) } -export const bind = Function.prototype.bind ? nativeBind : ownBind +export const bind = Function.prototype.bind + ? nativeBind + : polyfillBind /** * Convert an Array-like object to a real Array.