Skip to content

Commit

Permalink
fix: fix JS runtime detection priority
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit committed Oct 29, 2024
1 parent bb71e4e commit 05ddba5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ export default class Task extends EventTarget {
const promises: Promise<void>[] = []; // only for task level concurrency
while (
(totalTime < time
|| samples.length + limit.activeCount + limit.pendingCount < iterations)
|| samples.length + limit.activeCount + limit.pendingCount
< iterations)
&& !this.bench.signal?.aborted
) {
if (this.bench.concurrency === 'task') {
Expand Down
25 changes: 24 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
import { emptyFunction, tTable } from './constants';
import type { Fn, Statistics } from './types';

/**
* The JavaScript runtime environment.
*/
enum JSRuntime {
bun = 'bun',
deno = 'deno',
node = 'node',
browser = 'browser',
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
const isBun = !!(globalThis as any).Bun || !!globalThis.process.versions.bun;
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
const isDeno = !!(globalThis as any).Deno;
const isNode = globalThis.process.release.name === 'node';
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
const isBrowser = !!(globalThis as any).navigator;

const runtime: JSRuntime | 'unknown' = (() => {
if (isBun) return JSRuntime.bun;
if (isDeno) return JSRuntime.deno;
if (isNode) return JSRuntime.node;
if (isBrowser) return JSRuntime.browser;
return 'unknown';
})();

/**
* Converts nanoseconds to milliseconds.
*
Expand All @@ -21,7 +44,7 @@ export const nToMs = (ns: number) => ns / 1e6;
export const mToNs = (ms: number) => ms * 1e6;

let hrtimeBigint: () => bigint;
if (isBrowser) {
if (runtime === JSRuntime.browser) {
hrtimeBigint = () => {
throw new Error('hrtime.bigint() is not supported in this JS environment');
};
Expand Down

0 comments on commit 05ddba5

Please sign in to comment.