-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve sort algorithm (#2756)
* perf: improve sort algorithm * benchmark: add headers-length32.mjs * fix: benchmark * fix: fix performance regression for sorted arrays * test: add sorted test * refactor: simplify * refactor: remove comment
- Loading branch information
Showing
8 changed files
with
571 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { bench, run } from 'mitata' | ||
import { Headers } from '../lib/fetch/headers.js' | ||
|
||
const headers = new Headers( | ||
[ | ||
'Origin-Agent-Cluster', | ||
'RTT', | ||
'Accept-CH-Lifetime', | ||
'X-Frame-Options', | ||
'Sec-CH-UA-Platform-Version', | ||
'Digest', | ||
'Cache-Control', | ||
'Sec-CH-UA-Platform', | ||
'If-Range', | ||
'SourceMap', | ||
'Strict-Transport-Security', | ||
'Want-Digest', | ||
'Cross-Origin-Resource-Policy', | ||
'Width', | ||
'Accept-CH', | ||
'Via', | ||
'Refresh', | ||
'Server', | ||
'Sec-Fetch-Dest', | ||
'Sec-CH-UA-Model', | ||
'Access-Control-Request-Method', | ||
'Access-Control-Request-Headers', | ||
'Date', | ||
'Expires', | ||
'DNT', | ||
'Proxy-Authorization', | ||
'Alt-Svc', | ||
'Alt-Used', | ||
'ETag', | ||
'Sec-Fetch-User', | ||
'Sec-CH-UA-Full-Version-List', | ||
'Referrer-Policy' | ||
].map((v) => [v, '']) | ||
) | ||
|
||
const kHeadersList = Reflect.ownKeys(headers).find( | ||
(c) => String(c) === 'Symbol(headers list)' | ||
) | ||
|
||
const headersList = headers[kHeadersList] | ||
|
||
const kHeadersSortedMap = Reflect.ownKeys(headersList).find( | ||
(c) => String(c) === 'Symbol(headers map sorted)' | ||
) | ||
|
||
bench('Headers@@iterator', () => { | ||
headersList[kHeadersSortedMap] = null | ||
return [...headers] | ||
}) | ||
|
||
await run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { bench, group, run } from 'mitata' | ||
import { Headers } from '../lib/fetch/headers.js' | ||
|
||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' | ||
const charactersLength = characters.length | ||
|
||
function generateAsciiString (length) { | ||
let result = '' | ||
for (let i = 0; i < length; ++i) { | ||
result += characters[Math.floor(Math.random() * charactersLength)] | ||
} | ||
return result | ||
} | ||
|
||
const settings = { | ||
'fast-path (tiny array)': 4, | ||
'fast-path (small array)': 8, | ||
'fast-path (middle array)': 16, | ||
'fast-path': 32, | ||
'slow-path': 64 | ||
} | ||
|
||
for (const [name, length] of Object.entries(settings)) { | ||
const headers = new Headers( | ||
Array.from(Array(length), () => [generateAsciiString(12), '']) | ||
) | ||
|
||
const headersSorted = new Headers(headers) | ||
|
||
const kHeadersList = Reflect.ownKeys(headers).find( | ||
(c) => String(c) === 'Symbol(headers list)' | ||
) | ||
|
||
const headersList = headers[kHeadersList] | ||
|
||
const headersListSorted = headersSorted[kHeadersList] | ||
|
||
const kHeadersSortedMap = Reflect.ownKeys(headersList).find( | ||
(c) => String(c) === 'Symbol(headers map sorted)' | ||
) | ||
|
||
group(`length ${length} #${name}`, () => { | ||
bench('Headers@@iterator', () => { | ||
// prevention of memoization of results | ||
headersList[kHeadersSortedMap] = null | ||
return [...headers] | ||
}) | ||
|
||
bench('Headers@@iterator (sorted)', () => { | ||
// prevention of memoization of results | ||
headersListSorted[kHeadersSortedMap] = null | ||
return [...headersSorted] | ||
}) | ||
}) | ||
} | ||
|
||
await run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { bench, group, run } from 'mitata' | ||
import { sort, heapSort, introSort } from '../lib/fetch/sort.js' | ||
|
||
function compare (a, b) { | ||
return a < b ? -1 : 1 | ||
} | ||
|
||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' | ||
const charactersLength = characters.length | ||
|
||
function generateAsciiString (length) { | ||
let result = '' | ||
for (let i = 0; i < length; ++i) { | ||
result += characters[Math.floor(Math.random() * charactersLength)] | ||
} | ||
return result | ||
} | ||
|
||
const settings = { | ||
tiny: 32, | ||
small: 64, | ||
middle: 128, | ||
large: 512 | ||
} | ||
|
||
for (const [name, length] of Object.entries(settings)) { | ||
group(`sort (${name})`, () => { | ||
const array = Array.from(new Array(length), () => generateAsciiString(12)) | ||
// sort(array, compare) | ||
bench('Array#sort', () => array.slice().sort(compare)) | ||
bench('sort (intro sort)', () => sort(array.slice(), compare)) | ||
|
||
// sort(array, start, end, compare) | ||
bench('intro sort', () => introSort(array.slice(), 0, array.length, compare)) | ||
bench('heap sort', () => heapSort(array.slice(), 0, array.length, compare)) | ||
}) | ||
|
||
group(`sort sortedArray (${name})`, () => { | ||
const array = Array.from(new Array(length), () => generateAsciiString(12)).sort(compare) | ||
// sort(array, compare) | ||
bench('Array#sort', () => array.sort(compare)) | ||
bench('sort (intro sort)', () => sort(array, compare)) | ||
|
||
// sort(array, start, end, compare) | ||
bench('intro sort', () => introSort(array, 0, array.length, compare)) | ||
bench('heap sort', () => heapSort(array, 0, array.length, compare)) | ||
}) | ||
} | ||
|
||
await run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.