Skip to content

perf(js): String.eq compiles to === - #2075

Merged
nicolas-abril merged 1 commit into
bendlang:mainfrom
nicolas-abril:perf/js-string-eq
Nov 7, 2026
Merged

nicolas-abril merged 1 commit into
bendlang:mainfrom
nicolas-abril:perf/js-string-eq

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

On JS, Base's String.eq was compiled as written, Cmp.is_eq(String.order(a, b)): a loop that takes four slices and allocates a Cmp per character. It is now one === (a JS-only entry in OPERATIONS, like string_append; C keeps Base's def).

=== is exact, not a fast path: a String's JS text is canonical. char_new writes a Unicode scalar as itself and any other code as a lone \uDC00 marker followed by three units below 0x800, which fromCodePoint never produces, and a String literal is only a Lit when all its codes are scalars. So two Bend Strings are equal exactly when their JS texts are. (String.order cannot use < the same way: UTF-16 order is not code-point order.)

  • tests/base/string_eq.bend: empty, prefixes, astral chars, non-scalar codes (the marker form), surrogate pairs vs the astral char, built and sliced Strings vs literals; passes on interp, JS and C.
  • The 22 runnable tests that use String.eq/String.order print the same on JS; gates/repo.ts passes (comp.ts 65,977 -> 65,992 ttok).
  • 2M comparisons of 17-character strings on JS: 1.92 s -> 0.01 s.

Context: #2038. Backplane writes string dispatch as String.eq helper chains because string-literal patterns blow up compilation (#1266); this makes that workaround fast at runtime.

🤖 Generated with Claude Code

A String's JS text is canonical: char_new writes a scalar as itself and any
other code as a lone \uDC00 marker and three units below 0x800, so equal
Strings are equal texts. Base's String.eq walked both strings a character
at a time (four slices and a Cmp per character); now it is one ===. C
keeps Base's def.

2M comparisons of 17-character strings: 1.92 s -> 0.01 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril
nicolas-abril merged commit a5a60f1 into bendlang:main Nov 7, 2026
1 check passed
@nicolas-abril
nicolas-abril deleted the perf/js-string-eq branch November 7, 2026 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant