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(cleanupNumbericValues): improve how viewbox is split #2046

Merged
merged 1 commit into from
Jul 2, 2024

Conversation

SethFalco
Copy link
Member

@SethFalco SethFalco commented Jul 2, 2024

  1. Uses trim on the string instead of iterating the results after splitting.
  2. Reduces duplicate matching in the regex pattern.

The performance difference is minor, but measurable:

const data = [
  '0 0 120 120',
  '0 0 230 120',
  '110 0 120 120',
  '0 0 200.28423 200.28423',
  '20.000001 -19.99999 17.123456 70.708090',
  ' 0 0      150 100 ',
  '  0  0  0.5  .5  ',
];

function splitAndFilter(viewbox) {
  return viewbox
    .split(/(?:\s,?|,)\s*/g)
    .filter(v => v.length != 0)
    .map(Number);
};
// split and filter x 566,854 ops/sec ±0.58% (95 runs sampled)

function trimAndSplit(viewbox) {
  return viewbox
    .trim()
    .split(/(?:\s,?|,)\s*/g)
    .map(Number);
};
// trim and split x 583,812 ops/sec ±0.56% (96 runs sampled)

Related

@SethFalco SethFalco merged commit 9078e8c into svg:main Jul 2, 2024
12 checks passed
@SethFalco SethFalco deleted the chore-cleanup-numeric-values branch July 2, 2024 09:07
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