Skip to content

Instantly share code, notes, and snippets.

@BridgeAR
Created March 28, 2019 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BridgeAR/21b03f7e93eea56026b95e0531783d40 to your computer and use it in GitHub Desktop.
Save BridgeAR/21b03f7e93eea56026b95e0531783d40 to your computer and use it in GitHub Desktop.
V8 performance regression test
'use strict'
const testData = [ { _id: '5a660b85cc23d42973bb8f61',
index: 0,
guid: 'cbd5519b-24d2-479b-bd07-c6d676d350e7',
isActive: true,
balance: '$3,182.51',
picture: 'http://placehold.it/32x32',
age: 37,
eyeColor: 'brown',
name: 'Tami Burton',
gender: 'female',
company: 'PUSHCART',
email: 'tamiburton@pushcart.com',
phone: '+1 (821) 429-2754',
address: '272 Nichols Avenue, Brownlee, Iowa, 8202',
about:
'Incididunt aute irure ex dolore amet id dolor occaecat eu ullamco. Consectetur adipisicing ea Lorem laborum. Incididunt culpa nulla deserunt sit laboris pariatur occaecat est exercitation sit. Laboris non proident nulla do enim cillum laboris labore id est nisi commodo sint esse. Sit eiusmod ad elit consequat irure Lorem ex do id minim.\r\n',
registered: '2015-04-02T06:41:59 -02:00',
latitude: -49.91047,
longitude: 125.004934,
tags:
[ 'do', 'sint', 'tempor', 'enim', 'velit', 'eu', 'proident' ],
friends: [ [Object], [Object], [Object] ],
greeting: 'Hello, Tami Burton! You have 1 unread messages.',
favoriteFruit: 'strawberry' },
{ _id: '5a660b857751ef05c308268b',
index: 1,
guid: '1628176c-c9f6-4756-b605-5d5815a8c05d',
isActive: false,
balance: '$2,649.96',
picture: 'http://placehold.it/32x32',
age: 38,
eyeColor: 'brown',
name: 'Jami Buck',
gender: 'female',
company: 'BICOL',
email: 'jamibuck@bicol.com',
phone: '+1 (866) 562-3777',
address: '141 Gallatin Place, Geyserville, Alabama, 7547',
about:
'In occaecat veniam nisi do velit occaecat laboris cupidatat est voluptate. Labore quis id enim enim do tempor non ad dolor consectetur ea nisi. Eu cupidatat aute occaecat et in consequat aute nisi cupidatat est. Occaecat aliquip magna proident nostrud magna ad deserunt. Lorem Lorem ipsum irure laborum est Lorem mollit consequat eu. Et irure aliqua enim sit excepteur mollit.\r\n',
registered: '2015-08-21T04:06:37 -02:00',
latitude: -32.157219,
longitude: 167.849252,
tags:
[ 'aute', 'laborum', 'enim', 'ut', 'proident', 'aliqua', 'est' ],
friends: [ [Object], [Object], [Object] ],
greeting: 'Hello, Jami Buck! You have 9 unread messages.',
favoriteFruit: 'banana' },
{ _id: '5a660b8524acee2f78c68ad3',
index: 2,
guid: 'e6b479c7-995f-4742-a939-f7c6b03a9d84',
isActive: false,
balance: '$1,199.92',
picture: 'http://placehold.it/32x32',
age: 29,
eyeColor: 'blue',
name: 'Aisha Conner',
gender: 'female',
company: 'ZILPHUR',
email: 'aishaconner@zilphur.com',
phone: '+1 (806) 419-3132',
address: '815 Lafayette Avenue, Knowlton, Connecticut, 4266',
about:
'Culpa proident exercitation consequat amet do nisi qui dolore do exercitation ea. Officia esse est mollit cillum. Eu qui laboris minim sint pariatur. Esse occaecat est esse elit.\r\n',
registered: '2016-07-28T05:45:25 -02:00',
latitude: -25.704426,
longitude: -72.539193,
tags:
[ 'adipisicing', 'exercitation', 'sunt', 'est', 'ut', 'do', 'est' ],
friends: [ [Object], [Object], [Object] ],
greeting: 'Hello, Aisha Conner! You have 5 unread messages.',
favoriteFruit: 'strawberry' },
{ _id: '5a660b85d031eea36971a882',
index: 3,
guid: 'd52d9dcc-db48-445b-9c5f-b435fda26099',
isActive: true,
balance: '$3,059.34',
picture: 'http://placehold.it/32x32',
age: 22,
eyeColor: 'green',
name: 'Brooke Whitfield',
gender: 'female',
company: 'JIMBIES',
email: 'brookewhitfield@jimbies.com',
phone: '+1 (991) 538-3370',
address: '906 Lincoln Terrace, Bascom, Utah, 2326',
about:
'Lorem non sit mollit proident. Velit minim ex quis et amet proident officia ut elit ad est culpa sit mollit. Ullamco tempor adipisicing consectetur laborum esse minim laborum est exercitation nostrud eu. Fugiat sint consectetur velit reprehenderit. Ullamco pariatur mollit do proident anim quis tempor elit. In est ad sit sint ea laborum minim.\r\n',
registered: '2017-02-22T02:41:41 -01:00',
latitude: -33.567516,
longitude: -87.814848,
tags:
[ 'eu', 'eu', 'sit', 'incididunt', 'ea', 'laborum', 'ullamco' ],
friends: [ [Object], [Object], [Object] ],
greeting: 'Hello, Brooke Whitfield! You have 6 unread messages.',
favoriteFruit: 'strawberry' },
{ _id: '5a660b858f41af2de9dfd5be',
index: 4,
guid: '88a4bdd5-e4fa-440d-8345-50c149a636f9',
isActive: true,
balance: '$1,867.97',
picture: 'http://placehold.it/32x32',
age: 39,
eyeColor: 'brown',
name: 'Shauna Leonard',
gender: 'female',
company: 'XUMONK',
email: 'shaunaleonard@xumonk.com',
phone: '+1 (890) 549-3263',
address: '865 Folsom Place, Gorham, Pennsylvania, 9359',
about:
'Veniam ullamco elit do in velit tempor eiusmod eiusmod sit duis. Esse sit cupidatat culpa nisi mollit nisi ut nisi nulla nulla. Reprehenderit culpa anim ea dolore enim occaecat est. Incididunt incididunt dolor anim ullamco qui labore consequat exercitation elit sint incididunt culpa labore. Cupidatat proident duis tempor nostrud pariatur adipisicing. Tempor occaecat proident deserunt non irure irure. Lorem sint anim dolore exercitation cupidatat commodo proident labore irure commodo sunt.\r\n',
registered: '2016-12-17T08:40:42 -01:00',
latitude: -50.040955,
longitude: -39.104934,
tags: [ 'magna', 'et', 'ex', 'duis', 'ex', 'non', 'mollit' ],
friends: [ [Object], [Object], [Object] ],
greeting: 'Hello, Shauna Leonard! You have 9 unread messages.',
favoriteFruit: 'banana' } ]
var gap = ''
// eslint-disable-next-line
const strEscapeSequencesRegExp = /[\x00-\x1f\x22\x5c]/
// eslint-disable-next-line
const strEscapeSequencesReplacer = /[\x00-\x1f\x22\x5c]/g
// Escaped special characters. Use empty strings to fill up unused entries.
const meta = [
'\\u0000', '\\u0001', '\\u0002', '\\u0003', '\\u0004',
'\\u0005', '\\u0006', '\\u0007', '\\b', '\\t',
'\\n', '\\u000b', '\\f', '\\r', '\\u000e',
'\\u000f', '\\u0010', '\\u0011', '\\u0012', '\\u0013',
'\\u0014', '\\u0015', '\\u0016', '\\u0017', '\\u0018',
'\\u0019', '\\u001a', '\\u001b', '\\u001c', '\\u001d',
'\\u001e', '\\u001f', '', '', '\\"',
'', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '\\\\'
]
function escapeFn (str) {
return meta[str.charCodeAt(0)]
}
// Escape control characters, double quotes and the backslash.
// Note: it is faster to run this only once for a big string instead of only for
// the parts that it is necessary for. But this is only true if we do not add
// extra indentation to the string before.
function strEscape (str) {
// Some magic numbers that worked out fine while benchmarking with v8 6.0
if (str.length < 5000 && !strEscapeSequencesRegExp.test(str)) {
return str
}
if (str.length > 100) {
return str.replace(strEscapeSequencesReplacer, escapeFn)
}
var result = ''
var last = 0
for (var i = 0; i < str.length; i++) {
const point = str.charCodeAt(i)
if (point === 34 || point === 92 || point < 32) {
if (last === i) {
result = result + meta[point]
} else {
result = result + `${str.slice(last, i)}${meta[point]}`
}
last = i + 1
}
}
if (last === 0) {
result = str
} else if (last !== i) {
result = result + str.slice(last)
}
return result
}
// Simple without any options
function stringifySimple (key, value, stack) {
var i, res
switch (typeof value) {
case 'object':
if (value === null) {
return 'null'
}
if (typeof value.toJSON === 'function') {
value = value.toJSON(key)
// Prevent calling `toJSON` again
if (typeof value !== 'object') {
return stringifySimple(key, value, stack)
}
if (value === null) {
return 'null'
}
}
for (i = 0; i < stack.length; i++) {
if (stack[i] === value) {
return '"[Circular]"'
}
}
if (Array.isArray(value)) {
if (value.length === 0) {
return '[]'
}
stack.push(value)
res = '['
// Use null as placeholder for non-JSON values.
for (i = 0; i < value.length - 1; i++) {
const tmp = stringifySimple(i, value[i], stack)
res = res + (tmp !== undefined ? tmp : 'null')
res = res + ','
}
const tmp = stringifySimple(i, value[i], stack)
res = res + (tmp !== undefined ? tmp : 'null')
res = res + ']'
stack.pop()
return res
}
const keys = insertSort(Object.keys(value))
if (keys.length === 0) {
return '{}'
}
stack.push(value)
var separator = ''
res = '{'
for (i = 0; i < keys.length; i++) {
key = keys[i]
const tmp = stringifySimple(key, value[key], stack)
if (tmp !== undefined) {
res = res + `${separator}"${strEscape(key)}":${tmp}`
separator = ','
}
}
res = res + '}'
stack.pop()
return res
case 'string':
return `"${strEscape(value)}"`
case 'number':
// JSON numbers must be finite. Encode non-finite numbers as null.
// Convert the numbers implicit to a string instead of explicit.
return isFinite(value) ? String(value) : 'null'
case 'boolean':
return value === true ? 'true' : 'false'
}
}
function insertSort (arr) {
for (var i = 1; i < arr.length; i++) {
const tmp = arr[i]
var j = i
while (j !== 0 && arr[j - 1] > tmp) {
arr[j] = arr[j - 1]
j--
}
arr[j] = tmp
}
return arr
}
function stringify (value) {
gap = ''
return stringifySimple('', value, [])
}
console.time()
for (var i = 0; i < 1e5; i++) {
stringify(testData)
}
console.timeEnd()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment