From bb4900d9ebf322e9d348c89311267ae1edd96441 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 21 Nov 2020 23:18:01 +0100 Subject: [PATCH] lib: refactor primordials.uncurryThis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is done to avoid creating an array and gain performance. Co-authored-by: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> PR-URL: https://github.com/nodejs/node/pull/36221 Refs: https://v8.dev/blog/v8-release-80#optimizing-higher-order-builtins Reviewed-By: Rich Trott Reviewed-By: Michaƫl Zasso --- lib/internal/per_context/primordials.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 0ff52255feff41..baaca6d273f86c 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -12,17 +12,11 @@ // `primordials.Object` where `primordials` is a lexical variable passed // by the native module compiler. -const ReflectApply = Reflect.apply; - -// This function is borrowed from the function with the same name on V8 Extras' -// `utils` object. V8 implements Reflect.apply very efficiently in conjunction -// with the spread syntax, such that no additional special case is needed for -// function calls w/o arguments. -// Refs: https://github.com/v8/v8/blob/d6ead37d265d7215cf9c5f768f279e21bd170212/src/js/prologue.js#L152-L156 -function uncurryThis(func) { - return (thisArg, ...args) => ReflectApply(func, thisArg, args); -} - +// `uncurryThis` is equivalent to `func => Function.prototype.call.bind(func)`. +// It is using `call.bind(bind, call)` to avoid using `Function.prototype.bind` +// after it may have been mutated by users. +const { bind, call } = Function.prototype; +const uncurryThis = call.bind(bind, call); primordials.uncurryThis = uncurryThis; function copyProps(src, dest) {