-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmarks: add microbenchmarks for new ES6 features
Adds new microbenchmarks for destructuring, rest params and default params. PR-URL: #6222 Reviewed-By: Brian White <mscdex@mscdex.net>
- Loading branch information
Showing
3 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
method: ['withoutdefaults', 'withdefaults'], | ||
millions: [100] | ||
}); | ||
|
||
function oldStyleDefaults(x, y) { | ||
x = x || 1; | ||
y = y || 2; | ||
assert.strictEqual(x, 1); | ||
assert.strictEqual(y, 2); | ||
} | ||
|
||
function defaultParams(x = 1, y = 2) { | ||
assert.strictEqual(x, 1); | ||
assert.strictEqual(y, 2); | ||
} | ||
|
||
function runOldStyleDefaults(n) { | ||
|
||
common.v8ForceOptimization(oldStyleDefaults); | ||
|
||
var i = 0; | ||
bench.start(); | ||
for (; i < n; i++) | ||
oldStyleDefaults(); | ||
bench.end(n / 1e6); | ||
} | ||
|
||
function runDefaultParams(n) { | ||
|
||
common.v8ForceOptimization(defaultParams); | ||
|
||
var i = 0; | ||
bench.start(); | ||
for (; i < n; i++) | ||
defaultParams(); | ||
bench.end(n / 1e6); | ||
} | ||
|
||
function main(conf) { | ||
const n = +conf.millions * 1e6; | ||
|
||
switch (conf.method) { | ||
case 'withoutdefaults': | ||
runOldStyleDefaults(n); | ||
break; | ||
case 'withdefaults': | ||
runDefaultParams(n); | ||
break; | ||
default: | ||
throw new Error('Unexpected method'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
method: ['swap', 'destructure'], | ||
millions: [100] | ||
}); | ||
|
||
function runSwapManual(n) { | ||
var i = 0, x, y, r; | ||
bench.start(); | ||
for (; i < n; i++) { | ||
x = 1, y = 2; | ||
r = x; | ||
x = y; | ||
y = r; | ||
assert.strictEqual(x, 2); | ||
assert.strictEqual(y, 1); | ||
} | ||
bench.end(n / 1e6); | ||
} | ||
|
||
function runSwapDestructured(n) { | ||
var i = 0, x, y; | ||
bench.start(); | ||
for (; i < n; i++) { | ||
x = 1, y = 2; | ||
[x, y] = [y, x]; | ||
assert.strictEqual(x, 2); | ||
assert.strictEqual(y, 1); | ||
} | ||
bench.end(n / 1e6); | ||
} | ||
|
||
function main(conf) { | ||
const n = +conf.millions * 1e6; | ||
|
||
switch (conf.method) { | ||
case 'swap': | ||
runSwapManual(n); | ||
break; | ||
case 'destructure': | ||
runSwapDestructured(n); | ||
break; | ||
default: | ||
throw new Error('Unexpected method'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
method: ['copy', 'rest', 'arguments'], | ||
millions: [100] | ||
}); | ||
|
||
function copyArguments() { | ||
var len = arguments.length; | ||
var args = new Array(len); | ||
for (var i = 0; i < len; i++) | ||
args[i] = arguments[i]; | ||
assert.strictEqual(args[0], 1); | ||
assert.strictEqual(args[1], 2); | ||
assert.strictEqual(args[2], 'a'); | ||
assert.strictEqual(args[3], 'b'); | ||
} | ||
|
||
function restArguments(...args) { | ||
assert.strictEqual(args[0], 1); | ||
assert.strictEqual(args[1], 2); | ||
assert.strictEqual(args[2], 'a'); | ||
assert.strictEqual(args[3], 'b'); | ||
} | ||
|
||
function useArguments() { | ||
assert.strictEqual(arguments[0], 1); | ||
assert.strictEqual(arguments[1], 2); | ||
assert.strictEqual(arguments[2], 'a'); | ||
assert.strictEqual(arguments[3], 'b'); | ||
} | ||
|
||
function runCopyArguments(n) { | ||
|
||
common.v8ForceOptimization(copyArguments, 1, 2, 'a', 'b'); | ||
|
||
var i = 0; | ||
bench.start(); | ||
for (; i < n; i++) | ||
copyArguments(1, 2, 'a', 'b'); | ||
bench.end(n / 1e6); | ||
} | ||
|
||
function runRestArguments(n) { | ||
|
||
common.v8ForceOptimization(restArguments, 1, 2, 'a', 'b'); | ||
|
||
var i = 0; | ||
bench.start(); | ||
for (; i < n; i++) | ||
restArguments(1, 2, 'a', 'b'); | ||
bench.end(n / 1e6); | ||
} | ||
|
||
function runUseArguments(n) { | ||
|
||
common.v8ForceOptimization(useArguments, 1, 2, 'a', 'b'); | ||
|
||
var i = 0; | ||
bench.start(); | ||
for (; i < n; i++) | ||
useArguments(1, 2, 'a', 'b'); | ||
bench.end(n / 1e6); | ||
} | ||
|
||
function main(conf) { | ||
const n = +conf.millions * 1e6; | ||
|
||
switch (conf.method) { | ||
case 'copy': | ||
runCopyArguments(n); | ||
break; | ||
case 'rest': | ||
runRestArguments(n); | ||
break; | ||
case 'arguments': | ||
runUseArguments(n); | ||
break; | ||
default: | ||
throw new Error('Unexpected method'); | ||
} | ||
} |