- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 block 作用域函数方面,ES6 相对于 ES5 有哪些优势?我的意思是,在这两种情况下,该 block 看起来非常相似,那么它有什么区别,性能方面哪种方法更好?
ES6 block
{
function foo() {
return 1;
}
foo() === 1;
{
function foo() {
return 2;
}
foo() === 2;
}
foo() === 1;
}
ES5 block
(function () {
var foo = function () {
return 1;
}
foo() === 1;
(function () {
var foo = function () {
return 2;
}
foo() === 2;
})();
foo() === 1;
})();
最佳答案
这里有一个测试来显示哪个最快:
document.getElementById('btn').addEventListener('click', ({ target }) => {
target.disabled = true;
target.innerHTML = "Running…";
const suite = new Benchmark.Suite();
document.getElementById('ES6').style.fontWeight = '';
document.getElementById('ES5').style.fontWeight = '';
suite.add('ES6', () => {
{
function foo() {
return 1;
}
foo() === 1;
{
function foo() {
return 2;
}
foo() === 2;
}
foo() === 1;
}
}).add('ES5', () => {
(function () {
var foo = function () {
return 1;
}
foo() === 1;
(function () {
var foo = function () {
return 2;
}
foo() === 2;
})();
foo() === 1;
})();
})
.on('cycle', ({target: bench}) => document.getElementById(bench.name).textContent = `${bench.name}: ~${Benchmark.formatNumber(bench.hz|0)} ops/sec (~${Benchmark.formatNumber(Math.round(1e9/bench.hz))} ns/op)`)
.on('complete', function() {
const el = document.getElementById(this.filter('fastest').map('name')[0]),
hz = this.filter('fastest').map('hz')[0],
others = this.filter('slowest').map('hz'),
avg = others.reduce((a, b) => a + b, 0) / others.length;
el.style.fontWeight = 'bold';
el.textContent += ` \u{1f451} ${Math.round((1 - avg / hz) * 100, 2)}% faster`;
target.disabled = false;
target.innerHTML = "Run";
})
.run({ 'async': true });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.2/benchmark.min.js"></script>
<ul>
<li id="ES6"></li>
<li id="ES5"></li>
</ul>
<button id="btn">Run</button>
我的计算机上的结果:
- ES6: ~3,896,305 ops/sec (~257 ns/op) 👑 40% faster
- ES5: ~2,425,847 ops/sec (~412 ns/op)
关于javascript - block 作用域函数: ECMAScript 6 vs ECMAScript 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41016098/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!