|
const assert = require('assert'); |
|
const lodash = require('lodash'); |
|
const suite = require('chuhai'); |
|
const Baby = require('babyparse'); |
|
const platform = require('platform'); |
|
const fetch = require('isomorphic-fetch'); |
|
|
|
/* const gz = fs.readFileSync('h1bs.csv.gz'); |
|
const text = zlib.gunzipSync(gz); |
|
const data = Baby.parse(String(text), { |
|
header: true, |
|
delimiter: ',', |
|
skipEmptyLines: true |
|
}).data.slice(0, 5000); */ |
|
|
|
console.log(`# ${platform.description}\n`); |
|
|
|
suite.cb('clone', s => { |
|
s.set('maxTime', 0.1); |
|
s.set('minSamples', 10); |
|
|
|
let copy = null; |
|
let data = null; |
|
|
|
s.cycle(() => { |
|
assert(copy !== null); |
|
assert(copy !== data); |
|
assert(copy[0] !== data[0]); |
|
assert.deepStrictEqual(copy, data); |
|
copy = null; |
|
}); |
|
|
|
s.bench('lodash.cloneDeep', () => { |
|
copy = lodash.cloneDeep(data); |
|
}); |
|
|
|
s.bench('.map + lodash.clone', () => { |
|
copy = data.map(d => lodash.clone(d)); |
|
}); |
|
|
|
s.bench('.map + lodash.assign', () => { |
|
copy = data.map(d => lodash.assign({}, d)); |
|
}); |
|
|
|
s.bench('JSON string/parse', () => { |
|
copy = JSON.parse(JSON.stringify(data)); |
|
}); |
|
|
|
s.bench('.map + Object.assign', () => { |
|
copy = data.map(d => Object.assign({}, d)); |
|
}); |
|
|
|
s.bench('.map + Object.assign Polyfill', () => { |
|
copy = data.map(d => objectAssignPolyfill({}, d)); |
|
}); |
|
|
|
s.bench('.map + Object.assign Polyfill (without checks)', () => { |
|
copy = data.map(d => objectAssignPolyfillYOLO({}, d)); |
|
}); |
|
|
|
return fetch('http://swizec.github.io/h1b-software-salaries/data/h1bs.csv') |
|
.then(response => { |
|
if (response.status >= 400) { |
|
throw new Error('Bad response from server'); |
|
} |
|
return response.text(); |
|
}) |
|
.then(text => { |
|
data = Baby.parse(text, { |
|
header: true, |
|
delimiter: ',', |
|
skipEmptyLines: true |
|
}).data.slice(0, 1000); |
|
s.run(); |
|
}); |
|
}); |
|
|
|
// from https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign |
|
function objectAssignPolyfill(target) { |
|
'use strict'; |
|
// We must check against these specific cases. |
|
if (target === undefined || target === null) { |
|
throw new TypeError('Cannot convert undefined or null to object'); |
|
} |
|
|
|
const output = Object(target); |
|
for (let index = 1; index < arguments.length; index++) { |
|
const source = arguments[index]; |
|
if (source !== undefined && source !== null) { |
|
for (const nextKey in source) { |
|
if (Object.prototype.hasOwnProperty.call(source, nextKey)) { |
|
output[nextKey] = source[nextKey]; |
|
} |
|
} |
|
} |
|
} |
|
return output; |
|
} |
|
|
|
function objectAssignPolyfillYOLO(output, source) { |
|
for (const nextKey in source) { |
|
if (Object.prototype.hasOwnProperty.call(source, nextKey)) { |
|
output[nextKey] = source[nextKey]; |
|
} |
|
} |
|
return output; |
|
} |