Skip to content

Commit a42afba

Browse files
author
LAPTOP-465HP6VI\Victor
committed
Added Uint8 and Uint16 typed array sorting algorithms, based on Counting Sort, which is ludicrous speed
1 parent fb7a87b commit a42afba

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

CountingSort.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
var HpcAlgorithms = HpcAlgorithms || {};
3+
4+
HpcAlgorithms.Sorting = (function()
5+
{
6+
var HistogramOneByteComponentUInt8 = function(inArray)
7+
{
8+
var numberOfBins = 256;
9+
10+
var count = new Array(numberOfBins);
11+
for (var b = 0; b < numberOfBins; b++)
12+
count[b] = 0;
13+
14+
for (var i = 0; i < inArray.length; i++)
15+
count[ inArray[i] ]++;
16+
17+
return count;
18+
}
19+
20+
/**
21+
* Counting Sort of typed unsigned byte array
22+
* This algorithm is in-place
23+
* @param {Array of bytes} inputArray Array of numbers, which must be a typed array of unsigned bytes
24+
* @return {Array of bytes} Sorted array of unsigned bytes
25+
*/
26+
var SortCountingUInt8 = function(inputArray)
27+
{
28+
var count = HistogramOneByteComponentUInt8(inputArray);
29+
30+
var startIndex = 0;
31+
for (var countIndex = 0; countIndex < count.length; countIndex++)
32+
{
33+
inputArray.fill(countIndex, startIndex, startIndex + count[countIndex]); // 2X faster than using a for loop to fill an array
34+
startIndex += count[countIndex];
35+
}
36+
return inputArray;
37+
}
38+
39+
var HistogramOneWordComponent = function(inArray)
40+
{
41+
var numberOfBins = 65536;
42+
43+
var count = new Array(numberOfBins);
44+
for (var b = 0; b < numberOfBins; b++)
45+
count[b] = 0;
46+
47+
for (var current = 0; current < inArray.length; current++)
48+
count[ inArray[current] ]++;
49+
50+
return count;
51+
}
52+
53+
/**
54+
* Counting Sort of typed unsigned short array typed array
55+
* This algorithm is in-place
56+
* @param {Array of unsigned short} inputArray Array of numbers, which must be a typed array of unsigned short
57+
* @return {Array of unsigned short} Sorted array of unsigned short numbers
58+
*/
59+
var SortCountingUInt16 = function(inputArray)
60+
{
61+
var count = HistogramOneWordComponent(inputArray);
62+
63+
var startIndex = 0;
64+
for (var countIndex = 0; countIndex < count.length; countIndex++)
65+
{
66+
inputArray.fill(countIndex, startIndex, startIndex + count[countIndex]);
67+
startIndex += count[countIndex];
68+
}
69+
return inputArray;
70+
}
71+
72+
return {
73+
SortCountingUInt8: SortCountingUInt8,
74+
SortCountingUInt16: SortCountingUInt16
75+
};
76+
})();

readme.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ Let us know what other algorithms could use acceleration or improvement.
66
# High Performance Computing Algorithms in JavaScript
77

88
Faster and Better Algorithms, starting with high performance sorting:
9-
- LSD Radix Sort for unsigned integer arrays. 20-30X faster than JavaScript's built-in array sort for arrays less than 35 Million.
9+
- LSD Radix Sort for unsigned 32-bit integer arrays. 20-30X faster than JavaScript's built-in array sort for arrays less than 35 Million.
1010
5-10X faster for arrays greater than 35 Million. This sort algorithm is not in-place, returning a new sorted array.
1111
Discussion, benchmarks and usage in https://duvanenko.tech.blog/2017/06/15/faster-sorting-in-javascript/
1212
- LSD Radix Sort for arrays of objects by an unsigned integer key. 15X faster than JavaScript's built-in .sort().
1313
This is a stable sort, while JavaScript built-in is not stable. This sort algorithm is not in-place, returning a new sorted array.
1414
Discussion, benchmarks and usage in https://duvanenko.tech.blog/2017/07/10/sorting-arrays-of-objects-in-javascript-with-radix-sort/
15+
- Counting Sort for Uint8 and Uint16 typed arrays. 40X faster for Uint8 arrays and 15X faster for Uint16 arrays.
16+
This sort algorithm is in-place.
1517

1618
When you benchmark these algorithms keep in mind that for the first few runs the basic JavaScript JIT compiler is used, providing fast
1719
start up time and giving time for the optimizing compiler to run. After these few runs the optimized code starts being used,
18-
revealing the full performance of LSD Radix Sort algorithms. See https://blog.sessionstack.com/how-javascript-works-inside-the-v8-engine-5-tips-on-how-to-write-optimized-code-ac089e62b12e
20+
revealing the full performance of LSD Radix Sort and Counting Sort algorithms. See https://blog.sessionstack.com/how-javascript-works-inside-the-v8-engine-5-tips-on-how-to-write-optimized-code-ac089e62b12e
1921
for more details about the JIT engine in Chrome.
2022

2123
# Examples

0 commit comments

Comments
 (0)