-
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.
- Loading branch information
Showing
7 changed files
with
493 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,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,59 @@ | ||
import { bench, group, run } from 'mitata' | ||
import { sort, binaryInsertionSort, heapSort, introSort } from '../lib/fetch/sort.js' | ||
// import { sort as timSort } from './tim-sort.mjs' | ||
|
||
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 (mixed sort)', () => sort(array.slice(), compare)) | ||
// bench('tim sort', () => timSort(array.slice(), compare, 0, array.length)) | ||
|
||
// 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)) | ||
|
||
// Do not run them in large arrays as they are slow. | ||
if (array.length <= 1000) bench('binary insertion sort', () => binaryInsertionSort(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 (mixed sort)', () => sort(array, compare)) | ||
// bench('tim sort', () => timSort(array, compare, 0, array.length)) | ||
|
||
// sort(array, start, end, compare) | ||
bench('intro sort', () => introSort(array, 0, array.length, compare)) | ||
bench('heap sort', () => heapSort(array, 0, array.length, compare)) | ||
|
||
// Do not run them in large arrays as they are slow. | ||
if (array.length <= 1000) bench('binary insertion sort', () => binaryInsertionSort(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.