diff --git a/bench/bun.lockb b/bench/bun.lockb index 5016112d98..d3b34c5dcc 100755 Binary files a/bench/bun.lockb and b/bench/bun.lockb differ diff --git a/bench/package.json b/bench/package.json index b91097050b..761fa8936e 100644 --- a/bench/package.json +++ b/bench/package.json @@ -12,7 +12,7 @@ "fast-glob": "3.3.1", "fdir": "^6.1.0", "mitata": "^0.1.6", - "string-width": "^7.0.0", + "string-width": "7.1.0", "zx": "^7.2.3" }, "scripts": { diff --git a/bench/snippets/string-width.mjs b/bench/snippets/string-width.mjs index 63e1f4ecb5..2f45d0ae48 100644 --- a/bench/snippets/string-width.mjs +++ b/bench/snippets/string-width.mjs @@ -3,41 +3,37 @@ import npmStringWidth from "string-width"; const bunStringWidth = globalThis?.Bun?.stringWidth; -bench("npm/string-width (ansi + emoji + ascii)", () => { - 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"); + } } }