rapidhash-js 1.0.4 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.2.0] - 2025-05-29
4
+
5
+ - Implement [rapidhash v2.2](https://github.com/Nicoshev/rapidhash/pull/23).
6
+ - **Breaking:** The functions `rapidhash()`, `rapidhash_fast()`, and `rapidhash_protected()` are now implemented with the v2.2 algorithm.
7
+
8
+ ## [2.0.0] - 2025-05-18
9
+
10
+ - Implement [rapidhash v2](https://github.com/Nicoshev/rapidhash/pull/16).
11
+
3
12
  ## [1.0.4] - 2024-10-07
4
13
 
5
14
  - Support both ESM and CommonJS modules.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # rapidhash-js
2
2
 
3
- [![npm version](https://badge.fury.io/js/rapidhash-js.png)](https://badge.fury.io/js/rapidhash-js)
3
+ [![npm version](https://badge.fury.io/js/rapidhash-js.svg)](https://badge.fury.io/js/rapidhash-js)
4
4
  [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/komiya-atsushi/rapidhash-js/test.yaml)](https://github.com/komiya-atsushi/rapidhash-js/actions/workflows/test.yaml)
5
5
  [![NPM License](https://img.shields.io/npm/l/rapidhash-js)](https://opensource.org/licenses/MIT)
6
6
 
@@ -18,25 +18,101 @@ npm install rapidhash-js
18
18
  ## Usage
19
19
 
20
20
  ```typescript
21
- import {rapidhash} from 'rapidhash-js';
21
+ import {
22
+ // The functions rapidhash, rapidhash_fast, and rapidhash_protected currently implement the v2.2 algorithm.
23
+ rapidhash,
24
+ rapidhash_fast,
25
+ rapidhash_protected,
26
+ // To use the old v1 algorithm, import rapidhash_v1 (or rapidhash_v1_fast, rapidhash_v1_protected) instead.
27
+ rapidhash_v1,
28
+ } from 'rapidhash-js';
22
29
 
23
30
  // rapidhash() returns a 64-bit hash value represented as bigint.
24
- console.log(rapidhash('hello world')); // Output: 17498481775468162579n
31
+ console.log(rapidhash('hello world')); // Output: 1722744455612372674n
25
32
 
26
33
  // You can specify a 64-bit seed value with the 'seed' option.
27
- console.log(rapidhash('hello world', {seed: 0x0123_4567_89AB_CDEFn})); // Output: 9400866032237060842n
34
+ console.log(rapidhash('hello world', {seed: 0x0123_4567_89AB_CDEFn})); // Output: 13409940373860458135n
28
35
 
29
36
  // You can specify the behavior of the rapid_mum() function
30
37
  // with the rapidMumBehaviour option (default is 'fast').
31
- console.log(rapidhash('hello world', {rapidMumBehaviour: 'protected'})); // Output: 16019612682927597028n
38
+ console.log(rapidhash('hello world', {rapidMumBehaviour: 'protected'})); // Output: 12365338091610779222n
32
39
 
33
40
  // Instead of specifying the rapidMumBehaviour option,
34
41
  // you can call rapidhash_fast() or rapidhash_protected().
35
42
  console.log(rapidhash_protected('hello world'));
36
43
  ```
37
44
 
45
+ ## Benchmark
46
+
47
+ <details>
48
+ <summary>Apple M3 Max</summary>
49
+
50
+ ```
51
+ [Benchmark Environment]
52
+ Node.js: v22.15.1
53
+ CPU: Apple M3 Max (16 cores, 2400 MHz)
54
+
55
+ [1-4 bytes]
56
+ [v1.0] rapidhash (fast) : 6,285,122.13 ops/s (159.11 ns/iter)
57
+ [v1.0] rapidhash (protected) : 5,358,496.06 ops/s (186.62 ns/iter)
58
+ [v2.0] rapidhash (fast) : 6,751,586.61 ops/s (148.11 ns/iter)
59
+ [v2.0] rapidhash (protected) : 5,503,585.53 ops/s (181.70 ns/iter)
60
+ [v2.2] rapidhash (fast) : 6,713,669.81 ops/s (148.95 ns/iter)
61
+ [v2.2] rapidhash (protected) : 5,474,441.63 ops/s (182.67 ns/iter)
62
+
63
+ [5-8 bytes]
64
+ [v1.0] rapidhash (fast) : 5,995,195.47 ops/s (166.80 ns/iter)
65
+ [v1.0] rapidhash (protected) : 5,129,620.76 ops/s (194.95 ns/iter)
66
+ [v2.0] rapidhash (fast) : 6,217,065.60 ops/s (160.85 ns/iter)
67
+ [v2.0] rapidhash (protected) : 5,326,035.89 ops/s (187.76 ns/iter)
68
+ [v2.2] rapidhash (fast) : 6,050,308.59 ops/s (165.28 ns/iter)
69
+ [v2.2] rapidhash (protected) : 5,344,429.93 ops/s (187.11 ns/iter)
70
+
71
+ [9-16 bytes]
72
+ [v1.0] rapidhash (fast) : 5,986,726.82 ops/s (167.04 ns/iter)
73
+ [v1.0] rapidhash (protected) : 5,154,979.68 ops/s (193.99 ns/iter)
74
+ [v2.0] rapidhash (fast) : 6,131,914.48 ops/s (163.08 ns/iter)
75
+ [v2.0] rapidhash (protected) : 5,321,633.57 ops/s (187.91 ns/iter)
76
+ [v2.2] rapidhash (fast) : 6,247,440.63 ops/s (160.07 ns/iter)
77
+ [v2.2] rapidhash (protected) : 5,351,510.81 ops/s (186.86 ns/iter)
78
+
79
+ [17-64 bytes]
80
+ [v1.0] rapidhash (fast) : 3,843,646.47 ops/s (260.17 ns/iter)
81
+ [v1.0] rapidhash (protected) : 3,312,988.79 ops/s (301.84 ns/iter)
82
+ [v2.0] rapidhash (fast) : 3,947,650.45 ops/s (253.32 ns/iter)
83
+ [v2.0] rapidhash (protected) : 3,390,062.69 ops/s (294.98 ns/iter)
84
+ [v2.2] rapidhash (fast) : 4,039,784.70 ops/s (247.54 ns/iter)
85
+ [v2.2] rapidhash (protected) : 3,400,339.38 ops/s (294.09 ns/iter)
86
+
87
+ [65-111 bytes]
88
+ [v1.0] rapidhash (fast) : 2,539,382.14 ops/s (393.80 ns/iter)
89
+ [v1.0] rapidhash (protected) : 2,156,257.28 ops/s (463.77 ns/iter)
90
+ [v2.0] rapidhash (fast) : 2,580,829.71 ops/s (387.47 ns/iter)
91
+ [v2.0] rapidhash (protected) : 2,186,335.35 ops/s (457.39 ns/iter)
92
+ [v2.2] rapidhash (fast) : 2,545,090.54 ops/s (392.91 ns/iter)
93
+ [v2.2] rapidhash (protected) : 2,189,080.78 ops/s (456.81 ns/iter)
94
+
95
+ [112-256 bytes]
96
+ [v1.0] rapidhash (fast) : 1,518,423.71 ops/s (658.58 ns/iter)
97
+ [v1.0] rapidhash (protected) : 1,274,445.41 ops/s (784.66 ns/iter)
98
+ [v2.0] rapidhash (fast) : 1,484,277.31 ops/s (673.73 ns/iter)
99
+ [v2.0] rapidhash (protected) : 1,258,892.94 ops/s (794.35 ns/iter)
100
+ [v2.2] rapidhash (fast) : 1,525,211.09 ops/s (655.65 ns/iter)
101
+ [v2.2] rapidhash (protected) : 1,293,585.65 ops/s (773.05 ns/iter)
102
+
103
+ [1M bytes]
104
+ [v1.0] rapidhash (fast) : 356.49 ops/s (2.81 ms/iter)
105
+ [v1.0] rapidhash (protected) : 296.16 ops/s (3.38 ms/iter)
106
+ [v2.0] rapidhash (fast) : 375.05 ops/s (2.67 ms/iter)
107
+ [v2.0] rapidhash (protected) : 309.03 ops/s (3.24 ms/iter)
108
+ [v2.2] rapidhash (fast) : 374.26 ops/s (2.67 ms/iter)
109
+ [v2.2] rapidhash (protected) : 309.25 ops/s (3.23 ms/iter)
110
+ ```
111
+
112
+ </details>
113
+
38
114
  ## License
39
115
 
40
116
  MIT License
41
117
 
42
- Copyright (c) 2024 KOMIYA Atsushi.
118
+ Copyright (c) 2025 KOMIYA Atsushi.
@@ -0,0 +1,91 @@
1
+ type RapidMumBehaviour = 'fast' | 'protected';
2
+ interface RapidhashOptions {
3
+ seed: bigint;
4
+ rapidMumBehaviour: RapidMumBehaviour;
5
+ }
6
+
7
+ /**
8
+ * Calculate a 64-bit hash value of the given message.
9
+ *
10
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
11
+ * @param options Options for modifying the hash calculation.
12
+ * @param options.seed {bigint} 64-bit unsigned seed value.
13
+ * @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.
14
+ */
15
+ declare function rapidhash_v1(message: string | Uint8Array | DataView, options?: Partial<RapidhashOptions>): bigint;
16
+ /**
17
+ * Calculate a 64-bit hash value of the given message.
18
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.
19
+ *
20
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
21
+ * @param options Options for modifying the hash calculation.
22
+ * @param options.seed {bigint} 64-bit unsigned seed value.
23
+ */
24
+ declare function rapidhash_v1_fast(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
25
+ /**
26
+ * Calculate a 64-bit hash value of the given message.
27
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.
28
+ *
29
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
30
+ * @param options Options for modifying the hash calculation.
31
+ * @param options.seed {bigint} 64-bit unsigned seed value.
32
+ */
33
+ declare function rapidhash_v1_protected(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
34
+
35
+ /**
36
+ * Calculate a 64-bit hash value of the given message.
37
+ *
38
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
39
+ * @param options Options for modifying the hash calculation.
40
+ * @param options.seed {bigint} 64-bit unsigned seed value.
41
+ * @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.
42
+ */
43
+ declare function rapidhash_v2_0(message: string | Uint8Array | DataView, options?: Partial<RapidhashOptions>): bigint;
44
+ /**
45
+ * Calculate a 64-bit hash value of the given message.
46
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.
47
+ *
48
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
49
+ * @param options Options for modifying the hash calculation.
50
+ * @param options.seed {bigint} 64-bit unsigned seed value.
51
+ */
52
+ declare function rapidhash_v2_0_fast(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
53
+ /**
54
+ * Calculate a 64-bit hash value of the given message.
55
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.
56
+ *
57
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
58
+ * @param options Options for modifying the hash calculation.
59
+ * @param options.seed {bigint} 64-bit unsigned seed value.
60
+ */
61
+ declare function rapidhash_v2_0_protected(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
62
+
63
+ /**
64
+ * Calculate a 64-bit hash value of the given message.
65
+ *
66
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
67
+ * @param options Options for modifying the hash calculation.
68
+ * @param options.seed {bigint} 64-bit unsigned seed value.
69
+ * @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.
70
+ */
71
+ declare function rapidhash_v2_2(message: string | Uint8Array | DataView, options?: Partial<RapidhashOptions>): bigint;
72
+ /**
73
+ * Calculate a 64-bit hash value of the given message.
74
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.
75
+ *
76
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
77
+ * @param options Options for modifying the hash calculation.
78
+ * @param options.seed {bigint} 64-bit unsigned seed value.
79
+ */
80
+ declare function rapidhash_v2_2_fast(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
81
+ /**
82
+ * Calculate a 64-bit hash value of the given message.
83
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.
84
+ *
85
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
86
+ * @param options Options for modifying the hash calculation.
87
+ * @param options.seed {bigint} 64-bit unsigned seed value.
88
+ */
89
+ declare function rapidhash_v2_2_protected(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
90
+
91
+ export { rapidhash_v2_2 as rapidhash, rapidhash_v2_2_fast as rapidhash_fast, rapidhash_v2_2_protected as rapidhash_protected, rapidhash_v1, rapidhash_v1_fast, rapidhash_v1_protected, rapidhash_v2_0, rapidhash_v2_0_fast, rapidhash_v2_0_protected, rapidhash_v2_2, rapidhash_v2_2_fast, rapidhash_v2_2_protected };
package/lib/index.d.ts ADDED
@@ -0,0 +1,91 @@
1
+ type RapidMumBehaviour = 'fast' | 'protected';
2
+ interface RapidhashOptions {
3
+ seed: bigint;
4
+ rapidMumBehaviour: RapidMumBehaviour;
5
+ }
6
+
7
+ /**
8
+ * Calculate a 64-bit hash value of the given message.
9
+ *
10
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
11
+ * @param options Options for modifying the hash calculation.
12
+ * @param options.seed {bigint} 64-bit unsigned seed value.
13
+ * @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.
14
+ */
15
+ declare function rapidhash_v1(message: string | Uint8Array | DataView, options?: Partial<RapidhashOptions>): bigint;
16
+ /**
17
+ * Calculate a 64-bit hash value of the given message.
18
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.
19
+ *
20
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
21
+ * @param options Options for modifying the hash calculation.
22
+ * @param options.seed {bigint} 64-bit unsigned seed value.
23
+ */
24
+ declare function rapidhash_v1_fast(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
25
+ /**
26
+ * Calculate a 64-bit hash value of the given message.
27
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.
28
+ *
29
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
30
+ * @param options Options for modifying the hash calculation.
31
+ * @param options.seed {bigint} 64-bit unsigned seed value.
32
+ */
33
+ declare function rapidhash_v1_protected(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
34
+
35
+ /**
36
+ * Calculate a 64-bit hash value of the given message.
37
+ *
38
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
39
+ * @param options Options for modifying the hash calculation.
40
+ * @param options.seed {bigint} 64-bit unsigned seed value.
41
+ * @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.
42
+ */
43
+ declare function rapidhash_v2_0(message: string | Uint8Array | DataView, options?: Partial<RapidhashOptions>): bigint;
44
+ /**
45
+ * Calculate a 64-bit hash value of the given message.
46
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.
47
+ *
48
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
49
+ * @param options Options for modifying the hash calculation.
50
+ * @param options.seed {bigint} 64-bit unsigned seed value.
51
+ */
52
+ declare function rapidhash_v2_0_fast(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
53
+ /**
54
+ * Calculate a 64-bit hash value of the given message.
55
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.
56
+ *
57
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
58
+ * @param options Options for modifying the hash calculation.
59
+ * @param options.seed {bigint} 64-bit unsigned seed value.
60
+ */
61
+ declare function rapidhash_v2_0_protected(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
62
+
63
+ /**
64
+ * Calculate a 64-bit hash value of the given message.
65
+ *
66
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
67
+ * @param options Options for modifying the hash calculation.
68
+ * @param options.seed {bigint} 64-bit unsigned seed value.
69
+ * @param options.rapidMumBehaviour {'fast'|'protected'} Alters behaviour of the rapid_mum function. Defaults to 'fast'.
70
+ */
71
+ declare function rapidhash_v2_2(message: string | Uint8Array | DataView, options?: Partial<RapidhashOptions>): bigint;
72
+ /**
73
+ * Calculate a 64-bit hash value of the given message.
74
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'fast'.
75
+ *
76
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
77
+ * @param options Options for modifying the hash calculation.
78
+ * @param options.seed {bigint} 64-bit unsigned seed value.
79
+ */
80
+ declare function rapidhash_v2_2_fast(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
81
+ /**
82
+ * Calculate a 64-bit hash value of the given message.
83
+ * This function is equivalent to rapidhash() with options.rapidMumBehaviour set to 'protected'.
84
+ *
85
+ * @param message {string|Uint8Array|DataView} The message to be hashed.
86
+ * @param options Options for modifying the hash calculation.
87
+ * @param options.seed {bigint} 64-bit unsigned seed value.
88
+ */
89
+ declare function rapidhash_v2_2_protected(message: string | Uint8Array | DataView, options?: Partial<Omit<RapidhashOptions, 'rapidMumBehaviour'>>): bigint;
90
+
91
+ export { rapidhash_v2_2 as rapidhash, rapidhash_v2_2_fast as rapidhash_fast, rapidhash_v2_2_protected as rapidhash_protected, rapidhash_v1, rapidhash_v1_fast, rapidhash_v1_protected, rapidhash_v2_0, rapidhash_v2_0_fast, rapidhash_v2_0_protected, rapidhash_v2_2, rapidhash_v2_2_fast, rapidhash_v2_2_protected };