Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
block scope optimization test
'use strict'
const Benchmark = require('benchmark')
function MASK() {
return 100
}
function MAX() {
return 10000
}
function es2015() {
let sum = 0
const mask = MASK()
const n = MAX()
for (let i = 0; i < n; ++i) {
let t = i ^ mask
sum += t
}
return sum
}
function es5() {
var sum = 0
var mask = MASK()
var n = MAX()
for (var i = 0; i < n; ++i) {
var t = i ^ mask
sum += t
}
return sum
}
function es5const() {
var sum = 0
const mask = MASK()
const n = MAX()
for (var i = 0; i < n; ++i) {
var t = i ^ mask
sum += t
}
return sum
}
function simpleLet() {
var sum = 0
const mask = MASK()
const n = MAX()
for (var i = 0; i < n; ++i) {
let t = i ^ mask
sum += t
}
return sum
}
function compoundLet1() {
let sum = 0
const mask = MASK()
const n = MAX()
for (var i = 0; i < n; ++i) {
var t = i ^ mask
sum += t
}
return sum
}
function compoundLet2() {
var sum = 0
const mask = MASK()
const n = MAX()
for (let i = 0; i < n; ++i) {
var t = i ^ mask
sum += t
}
return sum
}
var result = 49996536
console.assert(es2015(), result)
console.assert(es5(), result)
console.assert(es5const(), result)
console.assert(simpleLet(), result)
console.assert(compoundLet1(), result)
console.assert(compoundLet2(), result)
new Benchmark.Suite()
.add('es2015', es2015)
.add('es5', es5)
.add('es5const', es5const)
.add('simpleLet', simpleLet)
.add('compoundLet1', compoundLet1)
.add('compoundLet2', compoundLet2)
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true })
@hax

This comment has been minimized.

Copy link
Owner Author

commented May 3, 2016

Result -- Node 6.0.0

es2015 x 5,675 ops/sec ±1.19% (80 runs sampled)
es5 x 64,360 ops/sec ±0.77% (81 runs sampled)
es5const x 64,972 ops/sec ±0.72% (85 runs sampled)
simpleLet x 63,085 ops/sec ±1.43% (78 runs sampled)
compoundLet1 x 16,993 ops/sec ±1.74% (76 runs sampled)
compoundLet2 x 21,095 ops/sec ±0.89% (83 runs sampled)
Fastest is es5const

@hax

This comment has been minimized.

Copy link
Owner Author

commented May 3, 2016

https://jsfiddle.net/twy0q6p2/

Result -- Safari 9.1

es2015 x 130,939 ops/sec ±0.79% (62 runs sampled)
es5 x 133,038 ops/sec ±0.59% (60 runs sampled)
es5const x 131,454 ops/sec ±1.06% (63 runs sampled)
simpleLet x 121,025 ops/sec ±5.22% (58 runs sampled)
compoundLet1 x 133,140 ops/sec ±0.70% (62 runs sampled)
compoundLet2 x 132,900 ops/sec ±0.79% (62 runs sampled)
Fastest is es5,compoundLet1,compoundLet2,es5const

Result -- Chrome 52.0 canary

es2015 x 22,185 ops/sec ±0.81% (56 runs sampled)
es5 x 66,951 ops/sec ±0.85% (58 runs sampled)
es5const x 65,650 ops/sec ±1.23% (60 runs sampled)
simpleLet x 67,883 ops/sec ±0.59% (63 runs sampled)
compoundLet1 x 30,057 ops/sec ±0.62% (62 runs sampled)
compoundLet2 x 22,463 ops/sec ±0.63% (60 runs sampled)
Fastest is simpleLet,es5

Result -- Firefox Nightly 49.0a1

es2015 x 88,193 ops/sec ±0.95% (58 runs sampled)
es5 x 89,697 ops/sec ±0.57% (61 runs sampled)
es5const x 89,255 ops/sec ±0.78% (60 runs sampled)
simpleLet x 87,819 ops/sec ±1.41% (61 runs sampled)
compoundLet1 x 90,008 ops/sec ±0.57% (61 runs sampled)
compoundLet2 x 88,414 ops/sec ±1.03% (60 runs sampled)
Fastest is compoundLet1,es5,es5const

@hax

This comment has been minimized.

Copy link
Owner Author

commented May 31, 2016

I revisit this performance tests, and find that it's V8 special optimization which will utilize multi-core for loops in some cases (like apply SIMD automatically), so the profiling result differ by hardware (how many core you have). It seems let will forbidden such optimization.

Consider the programmers could use Worker, and SIMD, Atomics to explicitly utilize multi-core, I guess we'd better drop such optimization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.