MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 74 lines 1.4 kB view raw
1const now = () => 2 typeof performance !== 'undefined' && performance.now ? performance.now() : Date.now(); 3 4function bench(name, fn, iters = 1) { 5 fn(); 6 const t0 = now(); 7 for (let i = 0; i < iters; i++) fn(); 8 const dt = now() - t0; 9 const per = (dt / iters).toFixed(6); 10 console.log(`${name}: ${dt.toFixed(2)} ms total, ${per} ms/iter (${iters} iters)`); 11} 12 13const visibleSymbol = Symbol('visible'); 14const getterSymbol = Symbol('getter'); 15 16function makePlainSource() { 17 return { 18 a: 1, 19 b: 2, 20 c: 3, 21 d: 4, 22 e: 5, 23 f: 6, 24 }; 25} 26 27function makeMixedSource() { 28 return { 29 a: 1, 30 b: 2, 31 c: 3, 32 d: 4, 33 [visibleSymbol]: 5, 34 }; 35} 36 37function makeGetterSource() { 38 const obj = { 39 a: 1, 40 b: 2, 41 c: 3, 42 d: 4, 43 }; 44 45 Object.defineProperty(obj, getterSymbol, { 46 enumerable: true, 47 get() { 48 return 42; 49 }, 50 }); 51 52 return obj; 53} 54 55function spreadPlain() { 56 const src = makePlainSource(); 57 return { ...src }; 58} 59 60function spreadMixedSymbol() { 61 const src = makeMixedSource(); 62 return { ...src }; 63} 64 65function spreadSymbolGetter() { 66 const src = makeGetterSource(); 67 return { ...src }; 68} 69 70console.log('=== Object Spread Benchmark ===\n'); 71 72bench('spread plain object', spreadPlain, 200_000); 73bench('spread enumerable symbol', spreadMixedSymbol, 200_000); 74bench('spread enumerable symbol getter', spreadSymbolGetter, 100_000);