Skip to content

Commit

Permalink
add bench
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam-Tait committed Oct 7, 2024
1 parent 04b0e34 commit db5c445
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
28 changes: 28 additions & 0 deletions collections/sliding_windows_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { slidingWindows } from "./sliding_windows.ts";
import { slidingWindows as unstableSlidingWindows } from "./unstable_sliding_windows.ts";

const arraySize = [10, 100, 1_000, 10_000, 100_000, 1_000_000];

for (const len of arraySize) {
const array = Array.from({ length: len }, (_, i) => i);
const group = `${len} elements`;
const size = 7;

Deno.bench({
name: "slidingWindows",
group,
fn: () => {
slidingWindows(array, size);
},
});

Deno.bench({
name: "(unstable) slidingWindows",
group,
baseline: true,
fn: () => {
unstableSlidingWindows(array, size);
},
});
}
12 changes: 8 additions & 4 deletions collections/unstable_sliding_windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ export function slidingWindows<T>(
options: SlidingWindowsOptions = {},
): T[][] {
const { step = 1, partial = false } = options;
if(!Number.isInteger(size) || size <= 0) {
throw new RangeError(`Cannot create sliding windows: size must be a positive integer, current value is ${size}`)
if (!Number.isInteger(size) || size <= 0) {
throw new RangeError(
`Cannot create sliding windows: size must be a positive integer, current value is ${size}`,
);
}
if(!Number.isInteger(step) || step <= 0) {
throw new RangeError(`Cannot create sliding windows: step must be a positive integer, current value is ${step}`)
if (!Number.isInteger(step) || step <= 0) {
throw new RangeError(
`Cannot create sliding windows: step must be a positive integer, current value is ${step}`,
);
}
const array = Array.isArray(iterable) ? iterable : Array.from(iterable);
const len = array.length;
Expand Down

0 comments on commit db5c445

Please sign in to comment.