UNPKG

@jrc03c/js-math-tools

Version:
34 lines (30 loc) 1.34 kB
import { copy } from "./copy.mjs" import { expect, test } from "@jrc03c/fake-jest" import { forEach } from "./for-each.mjs" import { mean } from "./mean.mjs" import { normal } from "./normal.mjs" import { range } from "./range.mjs" import { time } from "./time.mjs" test("tests that the `forEach` function is faster than `Array.prototype.forEach`", () => { // NOTE: For some extremely weird reason that I don't understand, this test // fails when placed *after* the test below! I've confirmed that it only fails // (1) when run via `fake-jest` (2) when placed after the function below. It // does *not* fail when placed above the test below *or* when run via regular // Node invocation (i.e., just running this code on its own in a new script // (without the `expect` bit at the end)). const x = range(0, 1e7).map(() => Math.random()) const fn = v => Math.sqrt(v) const time1 = mean(range(0, 10).map(() => time(() => x.forEach(fn)))) const time2 = mean(range(0, 10).map(() => time(() => forEach(x, fn)))) expect(time2).toBeLessThan(time1) }) test("tests that the `forEach` function works as expected", () => { const a = normal(100) const b = copy(a) forEach(b, (v, i) => (b[i] *= 2)) expect(a.length).toBe(100) expect(b.length).toBe(100) a.forEach((v, i) => { expect(b[i]).toBeCloseTo(a[i] * 2) }) })