Skip to content

Instantly share code, notes, and snippets.

@gotwarlost
Last active June 29, 2016 04:08
Show Gist options
  • Save gotwarlost/bca1c73e7a5135ad0d143d72db59fc40 to your computer and use it in GitHub Desktop.
Save gotwarlost/bca1c73e7a5135ad0d143d72db59fc40 to your computer and use it in GitHub Desktop.
Babel generate slowness for object expression assignments in non-compact mode
$ ./node_modules/.bin/babel-node bin/test-render-perf.js --compact
Sample prog: var foo={s0:{start:{line:0,column:0},end:{line:1,column:20}},s1:{start:{line:1,column:0},end:{line:2,column:20}}};
Items: 2 , time: 1
Items: 4 , time: 1
Items: 8 , time: 4
Items: 16 , time: 6
Items: 32 , time: 6
Items: 64 , time: 3
Items: 128 , time: 4
Items: 256 , time: 12
Items: 512 , time: 19
Items: 1024 , time: 42
Items: 2048 , time: 55
Items: 4096 , time: 106
Items: 8192 , time: 201
Items: 16384 , time: 365
$ ./node_modules/.bin/babel-node bin/test-render-perf.js
Sample prog: var foo = {
s0: {
start: {
line: 0,
column: 0
},
end: {
line: 1,
column: 20
}
},
s1: {
start: {
line: 1,
column: 0
},
end: {
line: 2,
column: 20
}
}
};
Items: 2 , time: 8
Items: 4 , time: 3
Items: 8 , time: 10
Items: 16 , time: 6
Items: 32 , time: 14
Items: 64 , time: 27
Items: 128 , time: 105
Items: 256 , time: 366
Items: 512 , time: 1379
Items: 1024 , time: 5364
Items: 2048 , time: 21230
^C ### got tired waiting for it to return
import template from 'babel-template';
import generate from 'babel-generator';
import * as T from 'babel-types';
const assignTemplate = template(`
var VAR = DATA;
`);
// create a program that initializes a variable of the form
// var foo = { s0: { start: { line: 0, column: 0}, end: {line 1, column:0} }
// etc. where the number of keys is controlled by the items arg
function toProgram(items) {
let obj = {};
for (let i=0; i < items; i += 1) {
const key = 's' + i;
obj[key] = { start: { line: i, column: 0 }, end: { line: i + 1, column: 20 } };
}
const node = T.valueToNode(obj);
const v = assignTemplate({
VAR: T.identifier("foo"),
DATA: node
});
return {
'type': 'File',
program: { 'type': 'Program', 'sourceType': 'script', body: [ v ] }
};
}
const nopt = require('nopt');
const opts = {
"compact": Boolean
};
const parsed = nopt(opts, null, process.argv, 2);
const compact = parsed.compact;
const generateOptions = {
compact: compact
};
for (let i = 1; i < 15; i += 1) {
const n = Math.pow(2, i);
const prog = toProgram(n);
const start = new Date().getTime();
const codeMap = generate(prog, generateOptions, '');
const end = new Date().getTime();
if (i == 1) {
console.log('Sample prog:', codeMap.code);
}
console.log('Items:', n, ', time:', (end - start));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment