Siv*_*ddy 8 javascript typescript ecmascript-6 angular
我已经浏览了地图并过滤了功能,并且知道了for和foreach之间存在某种差异和优势.
好处:
无需编写太多代码和非常清晰的理解
与迭代相比,性能更好
地图和过滤器将如何表现更好?
感谢任何建议.
其他答案实际上解释了原因。这是一个简单的基准测试示例。使用链接运行测试。
发布结果以防链接过期。
JS For 循环
var foo1 = ['spray', 'limit', 'elite', 'exuberant', 'destruction','present'];
for(var i=0;i<foo1.length;i++){
console.log(foo1[i]);
}
Run Code Online (Sandbox Code Playgroud)
执行时间处理时间 :677 ms
JS地图
var foo2 = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
foo2.map((v,i)=>console.log(v));
Run Code Online (Sandbox Code Playgroud)
执行时间处理时间 :686 ms
JS过滤器
var foo3 = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = foo3.filter(word => word.length > 6);
console.log(result);
Run Code Online (Sandbox Code Playgroud)
执行时间处理时间 :259 ms
给出的执行时间是我测试时的,可能会发生变化。