- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
在对 Array.prototype.fill()
方法进行一些测试(macOS 上的 Chrome)时,它显然比简单地创建您自己的 慢了将近两倍(如果不是更慢的话) for
循环并填充您的数组。
显然在做类似的事情:
for( var i = 0; i < Array.length; i++) {
A[i] = 0;
}
对比
Array.fill(0);
Array.fill()
方法将花费约 210-250 毫秒来填充大小为 10000000 的数组,而 for
循环将花费约 70-90 毫秒。似乎 Array.fill()
方法可以重写为简单地使用直接循环,因为您始终知道初始索引和目标索引。
let arrayTest = new Array(10000000),
startTime,
endTime;
startTime = performance.now();
arrayTest.fill(0);
endTime = performance.now();
console.log("%sms", endTime - startTime);
arrayTest = new Array(10000000);
startTime = performance.now();
for (let i = 0; i < arrayTest.length; i++){
arrayTest[i] = 0;
}
endTime = performance.now();
console.log("%sms", endTime - startTime);
与我在本地测试时相比,以上实际上显示出更大的差异。
编辑:经过进一步测试后,我现在意识到,当切换到 Firefox 及其真正依赖于引擎时,差异会减少很多。我猜这主要是不同的 JavaScript 引擎优化循环与方法的结果。不过,似乎可以优化 Array.prototype.fill()
中的循环来解决这种差异。
最佳答案
结果与报告一致,即部分 Chrome 是用 JavaScript 编写的,并且依靠运行时分析和优化来提高性能。
我将测试代码打包在一个函数中,以便从可以加载到不同浏览器中的测试页面重复调用(这不是可运行的片段):
<!DOCTYPE html>
<html><head><meta charset="utf-8">
<title>Array.prototype.fill</title>
<script>
Array.prototype.customFill = function( value, start = 0, end = this.length) {
var count = end-start;
if( count > 0 && count === Math.floor(count)){
while( count--)
this[start++]=value;
}
return this;
}
function test() {
let arrayTest,
startTime,
endTime,
arraySize = 1000000;
arrayTest = new Array(arraySize);
startTime = performance.now();
for (let i = 0; i < arrayTest.length; i++){
arrayTest[i] = 0;
}
endTime = performance.now();
console.log("%sms (loop)", endTime - startTime);
arrayTest = new Array(arraySize);
startTime = performance.now();
arrayTest.fill(0);
endTime = performance.now();
console.log("%sms (fill)", endTime - startTime);
arrayTest = new Array(arraySize);
startTime = performance.now();
arrayTest.customFill(0);
endTime = performance.now();
console.log("%sms (custom fill)", endTime - startTime);
}
</script>
</head>
<body>
open the console and click <button type="button" onclick="test()">test</button>
</body>
</html>
可以调整阵列大小以适应所用设备的性能。
Windows 下的 Chrome 浏览器的结果显示循环的性能大幅提升,前两次测试点击测试。在第二次点击时,循环的时间似乎有所改善。在第三次单击时,循环和填充方法似乎都得到了优化,并且运行速度几乎相等,而且速度有所提高。重新加载页面后结果可重复。
我发现这与 Chrome 脚本优化策略一致,与 Chrome 的 Array.prototype.fill
是用 C++ 或类似语言编写的不一致。尽管 Array.prototype.fill.toString()
将函数体报告为“本地代码”,但并未说明它是用什么语言编写的。
为自定义填充方法添加计时,为提高速度而编写,并存储为 Array.prototype.customFill
。
Firefox 的计时与用脚本编写的 Array.prototype.fill
一致。 native 实现优于循环,并且通常(但不总是)比自定义填充方法更快。
Chrome 显示的时间也与 Array.prototype.fill
一致,是用某种经过优化的脚本编写的。测试的所有三种填充方法都显示在一两次测试点击后速度有所提高。
但是,自定义填充方法的启动速度比 Chrome 原生版本快十倍以上。您需要将无意义的代码放入自定义方法中,以使其足够慢以接近 native 方法的初始速度。相反,经过优化后, native 方法的速度大约是原来的两倍——用 JavaScript 编写的自定义方法从未得到同样程度的优化。
虽然 Chrome 的 Array.prototype.fill
方法可以用 JavaScript 编写,但似乎需要额外的解释来解释最初的缓慢和注意到的最终性能优化。
关于javascript - 为什么 Array.prototype.fill() 与 `for` 循环相比有如此大的性能差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48836753/
以下代码,我使用 chrome 浏览器控制台进行了检查: function A(){ this.a='a' } 这是一个构造函数。我已经将一个属性 b 赋给了 A 的原型(prototype)。
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 5年前关闭。 Improve this
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 3年前关闭。 Improve this
我已经开始阅读 The Pragmatic Programmer,我很喜欢并学习堆形式,但我很难理解示踪子弹和原型(prototype)之间的区别。跟踪项目符号是否像拥有应用程序的所有 View 但尚
尽管阅读了 StackOverflow 上的大多数文章,但我现在实际上对原型(prototype)非常困惑。 function Foo() { } Foo.prototype.speak = func
我正在阅读以下代码,并开始想知道 Rectangle.prototype = Object.create(Shape.prototype) 和 Rectangle.prototype = Shape.
我想知道它们之间的区别: childObj.prototype = Object.create(parentObj.prototype) 和 childObj.prototype = parentOb
这个问题在这里已经有了答案: Why wouldn't I use Child.prototype = Parent.Prototype rather than Child.prototype =
在 node.js 中导出原型(prototype)的首选方法是什么?您可以采用两种方法: 导出原型(prototype)本身 function A () { } module.exports = A
我正在学习 JavaScript,发现了两种分配原型(prototype)的方法。 第一个是A.prototype = B.prototype,第二个是A.prototype = new B() 例如
在一些构造函数的定义之后,例如 child ,我见过以下两种形式: Child.prototype = Parent.prototype; 或 Child.prototype = new Parent
我正在阅读一本关于 OOP javascript 的书,但被其中一个示例卡住了。 在示例代码的第一个版本中,Shape 的一个新实例构造函数被创建并且 toString方法被调用。 toString方
这个问题在这里已经有了答案: What should I connect to the child prototype property in JavaScript (2 个答案) 关闭 8 年前。
在进行原型(prototype)设计时,您在多大程度上放弃了最佳实践来支持代码和修复黑客攻击?当然,代码并不打算在完整的生产环境中保留。 补充:我正在研究一个用 Python 制作的相当大的半工作原型
我正在尝试使用 Prototype 更新隐藏表单字段的值。表单域: 我正在尝试使用原型(prototype)更新值: var additionalVal = ',2'; var itemId = $
我正在阅读How to Make a Javascript Library我发现了作者所说的一个观点: function _() { //Some obects and var
我想用一个新函数扩展“Number”类型,因此我必须定义一个原型(prototype)。当我想到这一点时,我得到了一堆问题: Number 是否既继承了 Object.prototype 又继承了 F
这里好像有区别... 假设我们有 function MyConstructor() {} MyConstructor 的[[Prototype]] 是Function.prototype,不是 MyC
有人建议 Derived.prototype = Object.create(Base.prototype); 优于 Derived.prototype = new Base(); (如 this S
我是一名优秀的程序员,十分优秀!