diff options
author | 2022-09-14 18:23:22 -0700 | |
---|---|---|
committer | 2022-09-14 18:23:22 -0700 | |
commit | a291c1676f9351a03ae1092993d762c3147cff49 (patch) | |
tree | f9f32abc2145d2896ad2bb4595110609663badf0 /test/bun.js/web-globals.test.js | |
parent | 7bfa302b75c2450a872dc6b5de0002a9c7959ea9 (diff) | |
download | bun-a291c1676f9351a03ae1092993d762c3147cff49.tar.gz bun-a291c1676f9351a03ae1092993d762c3147cff49.tar.zst bun-a291c1676f9351a03ae1092993d762c3147cff49.zip |
5x faster crypto.randomValues()
Diffstat (limited to 'test/bun.js/web-globals.test.js')
-rw-r--r-- | test/bun.js/web-globals.test.js | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/test/bun.js/web-globals.test.js b/test/bun.js/web-globals.test.js index 178f5dd00..c5740b0fc 100644 --- a/test/bun.js/web-globals.test.js +++ b/test/bun.js/web-globals.test.js @@ -48,18 +48,34 @@ test("MessageEvent", () => { it("crypto.getRandomValues", () => { var foo = new Uint8Array(32); - // run it once - var array = crypto.getRandomValues(foo); - expect(array).toBe(foo); - expect(array.reduce((sum, a) => (sum += a === 0), 0) != foo.length).toBe( - true - ); + // run it once buffered and unbuffered + { + var array = crypto.getRandomValues(foo); + expect(array).toBe(foo); + expect(array.reduce((sum, a) => (sum += a === 0), 0) != foo.length).toBe( + true + ); + } // run it again to check that the fast path works for (var i = 0; i < 9000; i++) { var array = crypto.getRandomValues(foo); expect(array).toBe(foo); } + + // run it on a large input + expect( + !!crypto.getRandomValues(new Uint8Array(8096)).find((a) => a > 0) + ).toBe(true); + + { + // any additional input into getRandomValues() makes it unbuffered + var array = crypto.getRandomValues(foo, "unbuffered"); + expect(array).toBe(foo); + expect(array.reduce((sum, a) => (sum += a === 0), 0) != foo.length).toBe( + true + ); + } }); it("crypto.randomUUID", () => { |