|
| 1 | +// A benchmark for different methods/libs for string decoding |
| 2 | +// As per #27 Array.from is faulty on react-native but its still here for refference |
| 3 | +const now = require('performance-now'); |
| 4 | +const forEach = require('lodash/forEach'); |
| 5 | +const min = require('lodash/min'); |
| 6 | +const range = require('lodash/range'); |
| 7 | +const runes = require('runes'); |
| 8 | +const punycode = require('punycode'); |
| 9 | +const chalk = require('chalk'); |
| 10 | + |
| 11 | +// 20 chars |
| 12 | +const chars = [ |
| 13 | + 'a', |
| 14 | + 'b', |
| 15 | + 'c', |
| 16 | + 'd', |
| 17 | + 'e', |
| 18 | + 'f', |
| 19 | + 'g', |
| 20 | + 'h', |
| 21 | + 'i', |
| 22 | + 'j', |
| 23 | + 'k', |
| 24 | + 'l', |
| 25 | + '#', |
| 26 | + '1', |
| 27 | + '2', |
| 28 | + '3', |
| 29 | + '?', |
| 30 | + '!', |
| 31 | + '@', |
| 32 | + ' ', |
| 33 | +]; |
| 34 | + |
| 35 | +// 10 emojis |
| 36 | +const emojis = [ |
| 37 | + '💙', |
| 38 | + '😺', |
| 39 | + '🎉', |
| 40 | + '💣', |
| 41 | + '✔️', |
| 42 | + '💯', |
| 43 | + '⚛️', |
| 44 | + '💩', |
| 45 | + '🚀', |
| 46 | + '🍵', |
| 47 | +]; |
| 48 | + |
| 49 | +// Generate a random constant lenght string from a given charcter pool |
| 50 | +const makeString = (length, pool) => { |
| 51 | + let string = ''; |
| 52 | + for (let i = 0; i < length; i++) { |
| 53 | + const index = Math.floor(Math.random() * 10000) % pool.length; |
| 54 | + string += pool[index]; |
| 55 | + } |
| 56 | + return string; |
| 57 | +}; |
| 58 | + |
| 59 | +// Get input array with random strings |
| 60 | +const getInput = pool => { |
| 61 | + const times = range(0, 10000); |
| 62 | + const strings = []; |
| 63 | + forEach(times, () => { |
| 64 | + strings.push(makeString(50, pool)); |
| 65 | + }); |
| 66 | + return strings; |
| 67 | +}; |
| 68 | + |
| 69 | +// Run a cb on each string from input array and return the time diff |
| 70 | +const emojiBench = ({ cb, input, label }) => { |
| 71 | + const arr = []; |
| 72 | + const start = now(); |
| 73 | + forEach(input, (string) => { |
| 74 | + arr.push(cb(string)); |
| 75 | + }); |
| 76 | + const time = now() - start; |
| 77 | + console.log(`[${chalk.yellow(label)}]: ${time}`); |
| 78 | + return time; |
| 79 | +}; |
| 80 | + |
| 81 | +// Define tested methods |
| 82 | +// In case of punycode it's required to also decode. |
| 83 | +// To be fair other test will concat back to string as well. |
| 84 | +const tests = [ |
| 85 | + { |
| 86 | + cb: string => Array.from(string).join(''), |
| 87 | + label: 'Array.from', |
| 88 | + }, |
| 89 | + { |
| 90 | + cb: string => runes(string).join(''), |
| 91 | + label: 'runes', |
| 92 | + }, |
| 93 | + { |
| 94 | + cb: string => punycode.ucs2.encode(punycode.ucs2.decode(string)), |
| 95 | + label: 'punycode', |
| 96 | + }, |
| 97 | +]; |
| 98 | + |
| 99 | +// Executes a single batch of tests, and adds some colors :) |
| 100 | +const batch = (input, label) => { |
| 101 | + console.log(`\nRunning: ${chalk.green(label)}`); |
| 102 | + const results = tests.map(test => |
| 103 | + emojiBench(Object.assign({}, test, { input })) |
| 104 | + ); |
| 105 | + const winner = min(results); |
| 106 | + console.log( |
| 107 | + `Best time ${chalk.green(winner)} - ${tests[results.indexOf(winner)].label}` |
| 108 | + ); |
| 109 | +}; |
| 110 | + |
| 111 | +// Run test benchmark |
| 112 | +console.log( |
| 113 | + `--- Emoji benchmark running on ${chalk.bold.yellow(process.title)} ${chalk.bold.green(process.version)} ---` |
| 114 | +); |
| 115 | + |
| 116 | +// 0 - 10 |
| 117 | +batch(getInput(emojis), 'Random strings with 100% emojis'); |
| 118 | +// 10 - 10 |
| 119 | +batch(getInput([...chars.slice(0, 10), ...emojis]), 'Random strings with 1/2 emojis'); |
| 120 | +// 20 - 10 |
| 121 | +batch(getInput([...chars, ...emojis]), 'Random strings with 1/3 emojis'); |
| 122 | +// 40 - 10 |
| 123 | +batch(getInput([...chars, ...emojis, ...chars]), 'Random strings with 1/5 emojis'); |
| 124 | +// 40 - 10 |
| 125 | +batch(getInput(chars), 'Random strings with no emojis'); |
0 commit comments