Skip to content

Commit

Permalink
Speed up stringifyNumber (#1717)
Browse files Browse the repository at this point in the history
I'm currently profiling my build setup and noticed that
`stringifyNumber` pops up here and there. Instead of going with the
double regex replace approach, we'll go with the faster approach in
`removeLeadingZero`.

In total the changes in this PR speed up the build by about `0.9s` in my
project.

<img width="798" alt="stringifyNumber"
src="https://user-images.githubusercontent.com/1062408/204154146-ce67d0c9-faf2-40a1-8186-8d34caea74a5.png">
  • Loading branch information
marvinhagemeister authored Nov 30, 2022
1 parent c641da2 commit aa5d667
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { removeLeadingZero } = require('./svgo/tools');

/**
* @typedef {import('./types').PathDataItem} PathDataItem
* @typedef {import('./types').PathDataCommand} PathDataCommand
Expand Down Expand Up @@ -249,7 +251,7 @@ const stringifyNumber = (number, precision) => {
number = Math.round(number * ratio) / ratio;
}
// remove zero whole from decimal number
return number.toString().replace(/^0\./, '.').replace(/^-0\./, '-.');
return removeLeadingZero(number);
};

/**
Expand Down

0 comments on commit aa5d667

Please sign in to comment.