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

perf: use replaceAll instead of replacing plus #9

Merged
merged 1 commit into from
Sep 5, 2022
Merged

Conversation

anonrig
Copy link
Owner

@anonrig anonrig commented Sep 5, 2022

Solves #1

@anonrig anonrig merged commit 5d3977e into main Sep 5, 2022
@anonrig anonrig deleted the perf/plus branch September 5, 2022 20:52
@Fdawgs
Copy link
Contributor

Fdawgs commented Sep 6, 2022

Is replaceAll actually faster in this instance?
From other benchmarks, it's slower, see RafaelGSS/nodejs-bench-operations

@anonrig
Copy link
Owner Author

anonrig commented Sep 6, 2022

The benchmarks are quite flaky but here's my benchmark on node 18.8.0. I hope this helps @Fdawgs

import benchmark from "cronometro";

const input = "1+2+3+4+5+6+7+8+9";

await benchmark(
  {
    replaceAll() {
      return input.replaceAll("+", " ");
    },
    replace() {
      return input.replace(/\+/g, " ");
    },
  },
  { warmup: true },
);

with a result:

╔══════════════╤═════════╤═══════════════════╤═══════════╗
║ Slower tests │ Samples │            Result │ Tolerance ║
╟──────────────┼─────────┼───────────────────┼───────────╢
║ replace      │   10000 │ 2783621.84 op/sec │  ± 9.02 % ║
╟──────────────┼─────────┼───────────────────┼───────────╢
║ Fastest test │ Samples │            Result │ Tolerance ║
╟──────────────┼─────────┼───────────────────┼───────────╢
║ replaceAll   │    7500 │ 3117273.06 op/sec │  ± 0.97 % ║
╚══════════════╧═════════╧═══════════════════╧═══════════╝

@Fdawgs
Copy link
Contributor

Fdawgs commented Sep 6, 2022

Thanks @anonrig!

@anonrig
Copy link
Owner Author

anonrig commented Sep 6, 2022

Thank you for asking!

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.

None yet

2 participants