跟随问题Extending String.prototype performance我真的很感兴趣,因为仅仅将 "use strict"
添加到 String.prototype
方法就可以将性能提高 10 倍。 explanation通过 bergi很短,没有向我解释。为什么两种几乎相同的方法之间存在如此巨大的差异,仅在顶部的 "use strict"
不同?你能更详细地解释一下这背后的理论吗?
String.prototype.count = function(char) {
var n = 0;
for (var i = 0; i < this.length; i++)
if (this[i] == char) n++;
return n;
};
String.prototype.count_strict = function(char) {
"use strict";
var n = 0;
for (var i = 0; i < this.length; i++)
if (this[i] == char) n++;
return n;
};
// Here is how I measued speed, using Node.js 6.1.0
var STR = '0110101110010110100111010011101010101111110001010110010101011101101010101010111111000';
var REP = 1e4;
console.time('proto');
for (var i = 0; i < REP; i++) STR.count('1');
console.timeEnd('proto');
console.time('proto-strict');
for (var i = 0; i < REP; i++) STR.count_strict('1');
console.timeEnd('proto-strict');
结果:
proto: 101 ms
proto-strict: 7.5 ms
最佳答案
In strict mode, the this
context is not forced to be an object.如果您在非对象上调用函数,this
将只是那个非对象。
相比之下,在非严格模式下,如果 this
上下文还不是一个对象,它总是先包装在一个对象中。例如,(42).toString()
首先将 42
包装在一个 Number
对象中,然后调用 Number.prototype.toString
将 Number
对象作为 this
上下文。在严格模式下,this
上下文保持不变,只是调用 Number.prototype.toString
并将 42
作为 this
上下文。
(function() {
console.log(typeof this);
}).call(42); // 'object'
(function() {
'use strict';
console.log(typeof this);
}).call(42); // 'number'
在您的情况下,非严格模式版本花费大量时间将原始 string
包装和解包到 String
对象包装器中并返回。另一方面,严格模式版本直接作用于原始 string
,这提高了性能。
关于javascript - 为什么 "use strict"在此示例中将性能提高 10 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38411552/