Raynos提出了一个问题:Why is Function.prototype.bind slow?,或许与您遇到的问题类似。
回答者Domenic给出了该问题的处理方式:
Based on http://jsperf.com/bind-vs-emulate/6, which adds the es5-shim version for comparison, it looks like the culprit is the extra branch and instanceof
that the bound version has to perform to test if it's being called as a constructor.
Each time the bound version is run, the code that gets executed is essentially:
-- ----- ---------- ------ - -- ----- -------- --- --- ------------ ----- --- ------ ---------- --- - ---- - ---- - ------ ------------- ----- ---------------------------------- -- -- ---- -- -- -- ---- ----- -- -- --- ---- --- -- - ---------- ------- --------- ------ -- ------- ------ -- - --------- --- ----- ------- -
In the V8 source code, this check appears (inside boundFunction
) as
-- --------------------- - ------ ----------------------------------- -
(Plaintext link to v8natives.js for when Google Code Search dies.)
It is a bit puzzling that, for Chrome 16 at least, the es5-shim version is still faster than the native version. And that other browsers have rather varying results for es5-shim vs. native. Speculation: maybe %_IsConstructCall()
is even slower than this instanceof bound
, perhaps due to crossing native/JS code boundaries. And perhaps other browsers have a much faster way of checking for a [[Construct]]
call.
希望本文对你有帮助,欢迎支持JavaScript中文网
来源:JavaScript中文网 ,转载请注明来源 本文地址:https://www.javascriptcn.com/post/28235