-
Notifications
You must be signed in to change notification settings - Fork 529
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 to improve performance for large arrays
- Loading branch information
Showing
5 changed files
with
285 additions
and
18 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,31 @@ | ||
import { bench, run } from 'mitata' | ||
import { Headers } from '../lib/fetch/headers.js' | ||
import symbols from '../lib/core/symbols.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 headers = new Headers( | ||
Array.from(Array(100), () => generateAsciiString(7)).map((v) => [v, '']) | ||
) | ||
|
||
const headersList = headers[symbols.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,43 @@ | ||
import { bench, group, run } from 'mitata' | ||
import { sort, binaryInsertionSort, heapSort, introSort } from '../lib/fetch/util.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 | ||
} | ||
|
||
function insertionSort (list, compare) { | ||
for (let i = 1, j = 0, l = list.length, value; i < l; ++i) { | ||
value = list[i] | ||
for (j = i - 1; j >= 0; --j) { | ||
if (compare(list[j], value) <= 0) break | ||
list[j + 1] = list[j] | ||
} | ||
list[j + 1] = value | ||
} | ||
return list | ||
} | ||
|
||
group('sort', () => { | ||
const array = new Array(20).fill(null).map(() => generateAsciiString(10)) | ||
// sort(array, compare) | ||
bench('sort', () => sort(array.slice(), compare)) | ||
bench('insertion sort', () => insertionSort(array.slice(), compare)) | ||
bench('binary insertion sort', () => binaryInsertionSort(array.slice(), compare)) | ||
bench('native', () => array.slice().sort(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)) | ||
}) | ||
|
||
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
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
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,41 @@ | ||
'use strict' | ||
|
||
const { describe, test } = require('node:test') | ||
const assert = require('node:assert') | ||
const { sort, heapSort, binaryInsertionSort } = require('../../lib/fetch/util') | ||
|
||
function generateRandomNumberArray (length) { | ||
const array = new Uint16Array(length) | ||
for (let i = 0; i < length; ++i) { | ||
array[i] = (65535 * Math.random()) | 0 | ||
} | ||
return array | ||
} | ||
|
||
describe('sort', () => { | ||
const compare = (a, b) => a - b | ||
|
||
test('binary insertion sort', () => { | ||
for (let i = 0; i <= 2000; ++i) { | ||
const array = generateRandomNumberArray(100) | ||
const expected = array.slice().sort(compare) | ||
assert.deepStrictEqual(binaryInsertionSort(array, compare), expected) | ||
} | ||
}) | ||
|
||
test('heap sort', () => { | ||
for (let i = 0; i <= 2000; ++i) { | ||
const array = generateRandomNumberArray(100) | ||
const expected = array.slice().sort(compare) | ||
assert.deepStrictEqual(heapSort(array, 0, array.length, compare), expected) | ||
} | ||
}) | ||
|
||
test('intro sort', () => { | ||
for (let i = 0; i <= 2000; ++i) { | ||
const array = generateRandomNumberArray(100) | ||
const expected = array.slice().sort(compare) | ||
assert.deepStrictEqual(sort(array, compare), expected) | ||
} | ||
}) | ||
}) |