v8函数内联优化 (v5)

Revision 5 of this benchmark created on


Description

猜测由于隔离手段导致v8优化失效 在控制台将Setup框内的代码执行一遍,再点击测试会得到差异相当大的结果

Setup

const _map = (arr, fn) => {
  const r = new Array(arr.length)
  for (let i = 0; i < arr.length; i++) r[i]=fn(arr[i]);
  return r
}

const stringClone = (fn, key) => {
	let _fn = stringClone.dict.get(key);
	if (_fn === undefined) {
    	_fn = new Function(`return (${fn.toString()})`)()
 		stringClone.dict.set(key, _fn)
 	}
 	return _fn
};
stringClone.dict = new WeakMap()

const mapH = (arr, fn) => stringClone(_map, fn)(arr, fn)

const each1 = (i) => i + 1
const each2 = (i) => i + 2
const arr = new Array(1000).fill(true)

Test runner

Ready to run.

Testing in
TestOps/sec
_map
var r = 0
for (let i = 0; i < 1000; i++){
	var a = _map(arr, each1).length;
	var b = _map(arr, each2).length;
	r = a + b
}
ready
mapH
var r = 0
for (let i = 0; i < 1000; i++){
	var a = mapH(arr, each1).length;
	var b = mapH(arr, each2).length;
	r = a + b
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.