From 2e7d77b3b097e824cb4c8e8a011533ccbf5cf51d Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Thu, 22 Feb 2024 19:18:09 -0800 Subject: [PATCH] Add bench --- bench/bun.lockb | Bin 69389 -> 69389 bytes bench/package.json | 2 +- bench/snippets/string-width.mjs | 52 +++++++++++++++----------------- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/bench/bun.lockb b/bench/bun.lockb index 5016112d98470ccd4bcc1688ce58b2403fa1084e..d3b34c5dcc1756f671181ca988079dd4acb5daf0 100755 GIT binary patch delta 155 zcmV;M0A&A-o&=4a1duKuh(1gd7^4HYNwHIEQ)IusE771i6T&uqH^6~|@Ut!ru}&5m zlOzu`vzQvqJwQl82?=PI?5H?%UA>==8Vwu^Y*zLxnU`Ez)&y=18KiA74d%$B%5Mt8 z)Do&fe)ym&4KNjJd@(EJ&sfAOE0eROSZrel0bV#RFfK8Z7`$QzH!d+QFaWcFyiX?q JF|*L)JuAj2Jb?fJ delta 155 zcmV;M0A&A-o&=4a1duKu81@PJYCGDLqKBhktWJom*$;J`z=diC;MUApox { - npmStringWidth("hello there! πŸ˜€\u001b[31mπŸ˜€πŸ˜€"); -}); +const stringWidth = bunStringWidth || npmStringWidth; +const formatter = new Intl.NumberFormat(); +const format = n => { + return formatter.format(n); +}; -bench("npm/string-width (ansi + emoji)", () => { - npmStringWidth("πŸ˜€\u001b[31mπŸ˜€πŸ˜€"); -}); +const inputs = [ + ["hello", "ascii"], + ["helloπŸ˜€", "ascii+emoji"], + ["[31mπŸ˜€πŸ˜€", "ansi+emoji"], + ["πŸ˜€helloπŸ˜€[31mπŸ˜€πŸ˜€πŸ˜€", "ansi+emoji+ascii"], +]; -bench("npm/string-width (ansi + ascii)", () => { - npmStringWidth("\u001b[31mhello there!"); -}); +const repeatCounts = [1, 10, 100, 1000, 5000]; -if (bunStringWidth) { - bench("Bun.stringWidth (ansi + emoji + ascii)", () => { - bunStringWidth("hello there! πŸ˜€\u001b[31mπŸ˜€πŸ˜€"); - }); +const maxInputLength = Math.max(...inputs.map(([input]) => input.repeat(Math.max(...repeatCounts)).length)); - bench("Bun.stringWidth (ansi + emoji)", () => { - bunStringWidth("πŸ˜€\u001b[31mπŸ˜€πŸ˜€"); - }); +for (const [input, textLabel] of inputs) { + for (let repeatCount of repeatCounts) { + const label = bunStringWidth ? "Bun.stringWidth" : "npm/string-width"; - bench("Bun.stringWidth (ansi + ascii)", () => { - bunStringWidth("\u001b[31mhello there!"); - }); + const str = input.repeat(repeatCount); + const name = `${label} ${format(str.length).padStart(format(maxInputLength).length, " ")} chars ${textLabel}`; - if (npmStringWidth("πŸ˜€\u001b[31mπŸ˜€πŸ˜€") !== bunStringWidth("πŸ˜€\u001b[31mπŸ˜€πŸ˜€")) { - console.error("string-width mismatch"); - } + bench(name, () => { + stringWidth(str); + }); - if (npmStringWidth("hello there! πŸ˜€\u001b[31mπŸ˜€πŸ˜€") !== bunStringWidth("hello there! πŸ˜€\u001b[31mπŸ˜€πŸ˜€")) { - console.error("string-width mismatch"); - } - - if (npmStringWidth("\u001b[31mhello there!") !== bunStringWidth("\u001b[31mhello there!")) { - console.error("string-width mismatch"); + if (bunStringWidth && bunStringWidth(str) !== npmStringWidth(str)) { + throw new Error("string-width mismatch"); + } } }