Skip to content

Commit

Permalink
perf: faster detection for large files that are not SVGs
Browse files Browse the repository at this point in the history
If you pass in a large Buffer, the `validate` method on the SVG type can take an extremely long time because it converts the entire Buffer into a string. An easy fix that should take care of this 99% of the time, is to first check for the presence of the bytes used to represent `<svg`. In my testing this is at least 100x faster than converting a large Buffer to a string.
  • Loading branch information
ForbesLindesay authored and netroy committed May 30, 2023
1 parent 5bc0120 commit 08389eb
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/types/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ function calculateByViewbox(attrs: IAttributes, viewbox: IAttributes): ISize {
}

export const SVG: IImage = {
validate: (input) => svgReg.test(toUTF8String(input)),
// Scan only the first kilo-byte to speed up the check on larger files
validate: (input) => svgReg.test(toUTF8String(input, 0, 1000)),

calculate(input) {
const root = toUTF8String(input).match(extractorRegExps.root)
Expand Down

0 comments on commit 08389eb

Please sign in to comment.