Created
September 24, 2018 14:03
-
-
Save makotom/f08298d4e9a6a8a0698109302d83fa8a 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
| setTimeout(() => { | |
| const testLength = 1 * 1000 * 1000; | |
| const testPhases = 5; | |
| const measurements = { | |
| forOf: { | |
| array: [], | |
| hash: [], | |
| map: [] | |
| }, | |
| forEach: { | |
| array: [], | |
| hash: [], | |
| map: [] | |
| }, | |
| for: { | |
| array: [], | |
| hash: [], | |
| map: [] | |
| } | |
| }; | |
| const testArray = []; | |
| const testHash = {}; | |
| const testMap = new Map(); | |
| for (let iter = 0; iter < testLength; iter += 1) { | |
| testArray.push(iter); | |
| testHash[`n${iter}`] = iter; | |
| testMap.set(iter, `n${iter}`); | |
| } | |
| for (let phase = 0; phase < testPhases; phase += 1) { | |
| console.log(`Phase ${phase + 1}`); | |
| { | |
| { | |
| const times = []; | |
| console.log('Testing for-of (array)'); | |
| times.push(Date.now()); | |
| { | |
| for (const elem of testArray) { | |
| void elem; | |
| } | |
| } | |
| times.push(Date.now()); | |
| measurements.forOf.array.push(times[1] - times[0]); | |
| } | |
| { | |
| const times = []; | |
| console.log('Testing for-of (hash)'); | |
| times.push(Date.now()); | |
| { | |
| const iterableHash = { | |
| [Symbol.iterator]() { | |
| return { | |
| props: Object.getOwnPropertyNames(testHash), | |
| iter: 0, | |
| next() { | |
| return ( | |
| this.iter < this.props.length | |
| ? { value: testHash[this.props[this.iter++]], done: false } | |
| : { value: void 0, done: true } | |
| ); | |
| } | |
| } | |
| } | |
| }; | |
| for (const elem of iterableHash) { | |
| void elem; | |
| } | |
| } | |
| times.push(Date.now()); | |
| measurements.forOf.hash.push(times[1] - times[0]); | |
| } | |
| { | |
| const times = []; | |
| console.log('Testing for-of (map)'); | |
| times.push(Date.now()); | |
| { | |
| for (const elem of testMap) { | |
| void elem; | |
| } | |
| } | |
| times.push(Date.now()); | |
| measurements.forOf.map.push(times[1] - times[0]); | |
| } | |
| } | |
| { | |
| { | |
| const times = []; | |
| console.log('Testing forEach (array)'); | |
| times.push(Date.now()); | |
| { | |
| testArray.forEach((elem) => { | |
| void elem; | |
| }); | |
| } | |
| times.push(Date.now()); | |
| measurements.forEach.array.push(times[1] - times[0]); | |
| } | |
| { | |
| const times = []; | |
| console.log('Testing forEach (hash)'); | |
| times.push(Date.now()); | |
| { | |
| Object.getOwnPropertyNames(testArray).forEach((elem) => { | |
| void elem; | |
| }); | |
| } | |
| times.push(Date.now()); | |
| measurements.forEach.hash.push(times[1] - times[0]); | |
| } | |
| { | |
| const times = []; | |
| console.log('Testing forEach (map)'); | |
| times.push(Date.now()); | |
| { | |
| testMap.forEach((elem) => { | |
| void elem; | |
| }); | |
| } | |
| times.push(Date.now()); | |
| measurements.forEach.map.push(times[1] - times[0]); | |
| } | |
| } | |
| { | |
| { | |
| const times = []; | |
| console.log('Testing for (array)'); | |
| times.push(Date.now()); | |
| { | |
| for (let iter = 0; iter < testArray.length; iter += 1) { | |
| void testArray[iter]; | |
| } | |
| } | |
| times.push(Date.now()); | |
| measurements.for.array.push(times[1] - times[0]); | |
| } | |
| { | |
| const times = []; | |
| const hashKeys = Object.getOwnPropertyNames(testHash); | |
| console.log('Testing for (hash)'); | |
| times.push(Date.now()); | |
| { | |
| for (let iter = 0; iter < hashKeys.length; iter += 1) { | |
| void testHash[hashKeys[iter]]; | |
| } | |
| } | |
| times.push(Date.now()); | |
| measurements.for.hash.push(times[1] - times[0]); | |
| } | |
| { | |
| const times = []; | |
| console.log('Testing for (map)'); | |
| times.push(Date.now()); | |
| { | |
| const mapKeys = testMap.keys(); | |
| for (let key = mapKeys.next(); key.done === false; key = mapKeys.next()) { | |
| void testMap.get(key.value); | |
| } | |
| } | |
| times.push(Date.now()); | |
| measurements.for.map.push(times[1] - times[0]); | |
| } | |
| } | |
| } | |
| console.log(JSON.stringify(measurements)); | |
| }, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment