Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor length function to utilize a pre-compiled RegExp #12251

Merged
merged 2 commits into from
Oct 20, 2023

Conversation

marco-carvalho
Copy link
Contributor

This PR refactors the length function by extracting the regular expression used for validation into a separate, pre-compiled RegExp. This change aims to improve the performance of the function, especially in scenarios where it's called frequently, by avoiding the overhead of compiling the RegExp each time the function is called.

@RobinMalfait RobinMalfait self-assigned this Oct 20, 2023
@RobinMalfait RobinMalfait merged commit bcfb38d into tailwindlabs:master Oct 20, 2023
@RobinMalfait
Copy link
Member

Thanks!

Made some synthetic micro performance benchmarks that at the end of the day don't matter in practice, but there is a speedup so that's useful. Thanks!

node bench.js
cpu: Apple M1 Max
runtime: node v20.1.0 (arm64-darwin)

benchmark                 time (avg)             (min … max)
------------------------------------------------------------
• RegExp performance
------------------------------------------------------------
baseline              327.74 ps/iter     (300 ps … 12.73 ns)
Pre-compiled          107.04 ns/iter (102.57 ns … 118.06 ns)
Compiled when needed    2.57 µs/iter     (2.53 µs … 2.67 µs)

summary for RegExp performance
  baseline
   326.59x faster than Pre-compiled
   7832.34x faster than Compiled when needed
bun bench.js
cpu: Apple M1 Max
runtime: bun 1.0.6 (arm64-darwin)

benchmark                 time (avg)             (min … max)
------------------------------------------------------------
• RegExp performance
------------------------------------------------------------
baseline              328.41 ps/iter      (300 ps … 5.42 ns)
Pre-compiled            3.45 ns/iter    (3.15 ns … 12.25 ns)
Compiled when needed  607.87 ns/iter (577.39 ns … 684.43 ns)

summary for RegExp performance
  baseline
   10.51x faster than Pre-compiled
   1850.95x faster than Compiled when needed

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.

2 participants