Skip to content

Commit e20b828

Browse files
authored
feat!: Use TextEncoder to convert strings (#497)
1 parent 86a37b7 commit e20b828

File tree

11 files changed

+582
-18
lines changed

11 files changed

+582
-18
lines changed

.github/workflows/test.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,29 @@ jobs:
6464

6565
# Ensure the repository is clean after build & test
6666
- run: git --no-pager diff --compact-summary --exit-code
67+
68+
test-performance:
69+
runs-on: ${{ matrix.os }}
70+
71+
strategy:
72+
matrix:
73+
node-version:
74+
# Test the latest node version here, move older versions to `test-old-node-versions`
75+
- 20.x
76+
77+
os:
78+
- ubuntu-latest
79+
# - windows-latest
80+
# - macos-latest
81+
82+
steps:
83+
- uses: actions/checkout@v4
84+
85+
- name: Use Node.js ${{ matrix.node-version }}
86+
uses: actions/setup-node@v4
87+
with:
88+
node-version: ${{ matrix.node-version }}
89+
90+
- run: npm ci
91+
92+
- run: npm run insight

README.md

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ npm install --save js-xxhash
2121

2222
### Pure JS
2323

24-
Uses an internal JS conversion of strings to a UTF-8 `Uint8Array`.
25-
For higher performance consider using dedicated converters in the examples for Node and Browser below.
24+
Internally it uses [TextEncoder](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) to convert strings to a UTF-8 `Uint8Array`s.
2625

2726
```typescript
2827
import { xxHash32 } from 'js-xxhash';
@@ -46,23 +45,97 @@ import { xxHash32 } from 'js-xxhash';
4645

4746
let seed = 0;
4847
let str = 'My text to hash 😊';
49-
let hashNum = xxHash32(Buffer.from(str, 'utf8'), seed);
48+
let hashNum = xxHash32(str, seed);
5049
console.log(hashNum.toString(16));
5150
```
5251

5352
### Browser
5453

55-
In a browser, you need to use a function or library to create a
56-
[`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)
57-
58-
Using [Browserify](http://browserify.org/) you can use it like this:
59-
6054
```typescript
55+
// Using a bundler
6156
import { xxHash32 } from 'js-xxhash';
57+
// Using a CDN like jsDelivr
58+
import { xxHash32 } from 'https://cdn.jsdelivr.net/npm/js-xxhash@{version}/index.mjs';
6259

63-
let textEncoder = new TextEncoder(); // Note TextEncoder is experimental
6460
let seed = 0;
6561
let str = 'My text to hash 😊';
66-
let hashNum = xxHash32(textEncoder.encode(str), seed);
62+
let hashNum = xxHash32(str, seed);
6763
console.log(hashNum.toString(16));
6864
```
65+
66+
# Performance
67+
68+
To evaluate performance this package was compared to:
69+
70+
- [xxhashjs](https://www.npmjs.com/package/xxhashjs)
71+
- [xxhash-wasm](https://www.npmjs.com/package/xxhash-wasm)
72+
73+
One average a lorem-ipsum "word" is between 5 and 6 characters.
74+
75+
## Performance for Strings
76+
77+
```
78+
Running Perf Suite: xxhash-string-words-10
79+
Evaluate xxhash performance with a string of 10 words.
80+
✔ js-xxhash string 731_148.14 ops/sec
81+
✔ xxhashjs string 432_753.87 ops/sec
82+
✔ xxhash-wasm string 3_381_907.91 ops/sec
83+
84+
Running Perf Suite: xxhash-string-words-100
85+
Evaluate xxhash performance with a string of 100 words.
86+
✔ js-xxhash string 420_458.19 ops/sec
87+
✔ xxhashjs string 124_443.56 ops/sec
88+
✔ xxhash-wasm string 2_289_457.63 ops/sec
89+
90+
Running Perf Suite: xxhash-string-words-1000
91+
Evaluate xxhash performance with a string of 1000 words.
92+
✔ js-xxhash string 74_861.33 ops/sec
93+
✔ xxhashjs string 16_656.57 ops/sec
94+
✔ xxhash-wasm string 729_339.20 ops/sec
95+
96+
Running Perf Suite: xxhash-string-words-10000
97+
Evaluate xxhash performance with a string of 10000 words.
98+
✔ js-xxhash string 6_293.40 ops/sec
99+
✔ xxhashjs string 551.90 ops/sec
100+
✔ xxhash-wasm string 90_170.30 ops/sec
101+
102+
Running Perf Suite: xxhash-string-words-100000
103+
Evaluate xxhash performance with a string of 100000 words.
104+
✔ js-xxhash string 709.30 ops/sec
105+
✔ xxhashjs string 40.05 ops/sec
106+
✔ xxhash-wasm string 8_093.17 ops/sec
107+
```
108+
109+
### Performance with a Uint8Array Buffer
110+
111+
```
112+
Running Perf Suite: xxhash-buffer-words-10
113+
Evaluate xxhash performance with a buffer containing a string of 10 words.
114+
✔ js-xxhash buffer 2_859_850.03 ops/sec
115+
✔ xxhashjs buffer 699_053.22 ops/sec
116+
✔ xxhash-wasm buffer 3_657_504.67 ops/sec
117+
118+
Running Perf Suite: xxhash-buffer-words-100
119+
Evaluate xxhash performance with a buffer containing a string of 100 words.
120+
✔ js-xxhash buffer 800_609.77 ops/sec
121+
✔ xxhashjs buffer 402_424.91 ops/sec
122+
✔ xxhash-wasm buffer 2_569_294.66 ops/sec
123+
124+
Running Perf Suite: xxhash-buffer-words-1000
125+
Evaluate xxhash performance with a buffer containing a string of 1000 words.
126+
✔ js-xxhash buffer 79_925.04 ops/sec
127+
✔ xxhashjs buffer 55_568.13 ops/sec
128+
✔ xxhash-wasm buffer 753_856.33 ops/sec
129+
130+
Running Perf Suite: xxhash-buffer-words-10000
131+
Evaluate xxhash performance with a buffer containing a string of 10000 words.
132+
✔ js-xxhash buffer 8_152.57 ops/sec
133+
✔ xxhashjs buffer 6_046.82 ops/sec
134+
✔ xxhash-wasm buffer 104_463.50 ops/sec
135+
136+
Running Perf Suite: xxhash-buffer-words-100000
137+
Evaluate xxhash performance with a buffer containing a string of 100000 words.
138+
✔ js-xxhash buffer 458.33 ops/sec
139+
✔ xxhashjs buffer 602.90 ops/sec
140+
✔ xxhash-wasm buffer 9_835.61 ops/sec
141+
```

index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { xxHash32 } from './dist/esm/index.js';

0 commit comments

Comments
 (0)