Benchmark proxy (#3109)

This commit is contained in:
Lion - dapplion
2021-11-10 11:15:18 +01:00
committed by GitHub
parent 91f3fef1c2
commit 5f94e69320

View File

@@ -0,0 +1,46 @@
import {itBench} from "@dapplion/benchmark";
import {expect} from "chai";
describe("Proxy cost", () => {
const n = 100_000;
const array: number[] = [];
for (let i = 0; i < n; i++) {
array.push(i);
}
const arrayWithProxy = new Proxy(array, {
get(target, p) {
if (p === "length") {
return target.length;
} else {
return target[(p as unknown) as number];
}
},
});
const wrappedArray = {
array,
get(i: number) {
return this.array[i];
},
};
it("Check is correct", () => {
for (const i of [0, 1, Math.floor(n / 2)]) {
expect(array[i]).to.equal(i, `Wrong value array[${i}]`);
expect(arrayWithProxy[i]).to.equal(i, `Wrong value arrayWithProxy[${i}]`);
}
});
itBench(`regular array get ${n} times`, () => {
for (let i = 0; i < n; i++) array[i];
});
itBench(`wrappedArray get ${n} times`, () => {
for (let i = 0; i < n; i++) wrappedArray.get(i);
});
itBench(`arrayWithProxy get ${n} times`, () => {
for (let i = 0; i < n; i++) arrayWithProxy[i];
});
});