Skip to content

Commit

Permalink
Avoid n^2 complexity during decoding
Browse files Browse the repository at this point in the history
`push` using `Array.concat` creates a new copy of the array for each
inserted element and leads to n^2 complexity when decoding arrays.
For pprof with large numbers of samples, this quickly leads to very long
decoding (>1 min) compared to ~1s with this change.
  • Loading branch information
nsavoire committed Feb 22, 2023
1 parent 14e0f30 commit fd5f5a6
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ function decodeNumbers(buffer: Uint8Array): Array<Numeric> {
}

function push<T>(value: T, list?: Array<T>): Array<T> {
return Array.isArray(list) ? list.concat(value) : [value]
if (list == null) {
return [value]
}
list.push(value)
return list
}

function measureNumber(number: Numeric): number {
Expand Down

0 comments on commit fd5f5a6

Please sign in to comment.