Created
July 1, 2016 05:14
-
-
Save dtinth/87d79cedb0f56edfb665d65edea03dba to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict' | |
console.log('| items | Embedded literal | Runtime JSON.parse |') | |
console.log('| -----:| ----------------:| ------------------:|') | |
for (let n = 1; n <= 65536; n <<= 1) { | |
test(n) | |
} | |
function test (items) { | |
const data = generateTestData(items) | |
const codev1 = '(function (x) { return ' + JSON.stringify(data) + ' })' | |
const fn1 = eval(codev1) | |
const codev2 = '(function (x) { return JSON.parse(' + JSON.stringify(JSON.stringify(data)) + ') })' | |
const fn2 = eval(codev2) | |
const output = [ items, measure(fn1), measure(fn2) ] | |
console.log('| ' + output.join(' | ') + ' |') | |
} | |
function generateTestData (items) { | |
const 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 } } | |
} | |
return obj | |
} | |
function measure (fn) { | |
let sum = 0 | |
for (let i = 0; i < 10; i ++) { | |
const start = Date.now() | |
fn() | |
const finish = Date.now() | |
sum += finish - start | |
} | |
return sum / 10 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment