Skip to content

Commit

Permalink
⚡️ Faster initialization of globals by dropping typeof checks (#5617)
Browse files Browse the repository at this point in the history
**Description**

<!-- Please provide a short description and potentially linked issues
justifying the need for this PR -->

**⚠️ Minor breaking change**

In the past we used to checks for `typeof Type` when initializing our
globals (the globals protecting fast-check from poisoning). It used to
be done because some of them only appeared starting at Node >8. With our
minimal target version being bumped to at least 10.5.0 we can safely
stop having these checks and save some precious time during tests (for
our users).

This change forces us to move minimal requirements to at least 10.5.0.

<!-- * Your PR is fixing a bug or regression? Check for existing issues
related to this bug and link them -->
<!-- * Your PR is adding a new feature? Make sure there is a related
issue or discussion attached to it -->

<!-- You can provide any additional context to help into understanding
what's this PR is attempting to solve: reproduction of a bug, code
snippets... -->

**Checklist** — _Don't delete this checklist and make sure you do the
following before opening the PR_

- [x] The name of my PR follows [gitmoji](https://gitmoji.dev/)
specification
- [x] My PR references one of several related issues (if any)
- [x] New features or breaking changes must come with an associated
Issue or Discussion
- [x] My PR does not add any new dependency without an associated Issue
or Discussion
- [x] My PR includes bumps details, please run `yarn bump` and flag the
impacts properly
- [x] My PR adds relevant tests and they would have failed without my PR
(when applicable)

<!-- More about contributing at
https://github.com/dubzzz/fast-check/blob/main/CONTRIBUTING.md -->

**Advanced**

<!-- How to fill the advanced section is detailed below! -->

- [x] Category: ⚡️ Improve performance
- [x] Impacts: We have to run against Node 10.5.0+

<!-- [Category] Please use one of the categories below, it will help us
into better understanding the urgency of the PR --> <!-- * ✨ Introduce
new features -->
<!-- * 📝 Add or update documentation -->
<!-- * ✅ Add or update tests -->
<!-- * 🐛 Fix a bug -->
<!-- * 🏷️ Add or update types -->
<!-- * ⚡️ Improve performance -->
<!-- * _Other(s):_ ... -->
  • Loading branch information
dubzzz authored Jan 14, 2025
1 parent fc50724 commit 1bf1eb0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/fast-check/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Here are the minimal requirements to use fast-check properly without any polyfil

| fast-check | node | ECMAScript version | _TypeScript (optional)_ |
| ---------- | ------------------- | ------------------ | ----------------------- |
| **4.x** |8<sup>(1)</sup> | ES2017 | ≥5.0 |
| **4.x** |10.5.0 | ES2020 | ≥5.0 |
| **3.x** | ≥8<sup>(1)</sup> | ES2017 | ≥4.1<sup>(2)</sup> |
| **2.x** | ≥8<sup>(1)</sup> | ES2017 | ≥3.2<sup>(3)</sup> |
| **1.x** | ≥0.12<sup>(1)</sup> | ES3 | ≥3.0<sup>(3)</sup> |
Expand Down
2 changes: 1 addition & 1 deletion packages/fast-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"e2e": "vitest --config vitest.e2e.config.mjs",
"update:documentation": "cross-env UPDATE_CODE_SNIPPETS=true vitest --config vitest.documentation.config.mjs",
"test-bundle": "node test-bundle/run.cjs && node test-bundle/run.mjs && node test-bundle/run-advanced.cjs",
"test-legacy-bundle": "nvs add 8 && $(nvs which 8) test-bundle/run.cjs && $(nvs which 8) test-bundle/run-advanced.cjs",
"test-legacy-bundle": "nvs add 10.5 && $(nvs which 10.5) test-bundle/run.cjs && $(nvs which 10.5) test-bundle/run-advanced.cjs",
"docs": "api-extractor run --local && rm docs/fast-check.api.json && typedoc --out docs src/fast-check-default.ts && node postbuild/main.mjs",
"docs-ci": "cross-env EXPECT_GITHUB_SHA=true yarn docs",
"docs:serve": "yarn dlx serve docs/"
Expand Down
62 changes: 20 additions & 42 deletions packages/fast-check/src/utils/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,45 @@ import { safeApply } from './apply';

// Globals

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SArray: typeof Array = typeof Array !== 'undefined' ? Array : undefined!;
const SArray: typeof Array = Array;
export { SArray as Array };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SBigInt: typeof BigInt = typeof BigInt !== 'undefined' ? BigInt : undefined!;
const SBigInt: typeof BigInt = BigInt;
export { SBigInt as BigInt };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SBigInt64Array: typeof BigInt64Array = typeof BigInt64Array !== 'undefined' ? BigInt64Array : undefined!;
const SBigInt64Array: typeof BigInt64Array = BigInt64Array;
export { SBigInt64Array as BigInt64Array };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SBigUint64Array: typeof BigUint64Array = typeof BigUint64Array !== 'undefined' ? BigUint64Array : undefined!;
const SBigUint64Array: typeof BigUint64Array = BigUint64Array;
export { SBigUint64Array as BigUint64Array };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SBoolean: typeof Boolean = typeof Boolean !== 'undefined' ? Boolean : undefined!;
const SBoolean: typeof Boolean = Boolean;
export { SBoolean as Boolean };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SDate: typeof Date = typeof Date !== 'undefined' ? Date : undefined!;
const SDate: typeof Date = Date;
export { SDate as Date };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SError: typeof Error = typeof Error !== 'undefined' ? Error : undefined!;
const SError: typeof Error = Error;
export { SError as Error };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SFloat32Array: typeof Float32Array = typeof Float32Array !== 'undefined' ? Float32Array : undefined!;
const SFloat32Array: typeof Float32Array = Float32Array;
export { SFloat32Array as Float32Array };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SFloat64Array: typeof Float64Array = typeof Float64Array !== 'undefined' ? Float64Array : undefined!;
const SFloat64Array: typeof Float64Array = Float64Array;
export { SFloat64Array as Float64Array };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SInt8Array: typeof Int8Array = typeof Int8Array !== 'undefined' ? Int8Array : undefined!;
const SInt8Array: typeof Int8Array = Int8Array;
export { SInt8Array as Int8Array };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SInt16Array: typeof Int16Array = typeof Int16Array !== 'undefined' ? Int16Array : undefined!;
const SInt16Array: typeof Int16Array = Int16Array;
export { SInt16Array as Int16Array };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SInt32Array: typeof Int32Array = typeof Int32Array !== 'undefined' ? Int32Array : undefined!;
const SInt32Array: typeof Int32Array = Int32Array;
export { SInt32Array as Int32Array };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SNumber: typeof Number = typeof Number !== 'undefined' ? Number : undefined!;
const SNumber: typeof Number = Number;
export { SNumber as Number };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SString: typeof String = typeof String !== 'undefined' ? String : undefined!;
const SString: typeof String = String;
export { SString as String };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SSet: typeof Set = typeof Set !== 'undefined' ? Set : undefined!;
const SSet: typeof Set = Set;
export { SSet as Set };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SUint8Array: typeof Uint8Array = typeof Uint8Array !== 'undefined' ? Uint8Array : undefined!;
const SUint8Array: typeof Uint8Array = Uint8Array;
export { SUint8Array as Uint8Array };
const SUint8ClampedArray: typeof Uint8ClampedArray =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : undefined!;
const SUint8ClampedArray: typeof Uint8ClampedArray = Uint8ClampedArray;
export { SUint8ClampedArray as Uint8ClampedArray };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SUint16Array: typeof Uint16Array = typeof Uint16Array !== 'undefined' ? Uint16Array : undefined!;
const SUint16Array: typeof Uint16Array = Uint16Array;
export { SUint16Array as Uint16Array };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const SUint32Array: typeof Uint32Array = typeof Uint32Array !== 'undefined' ? Uint32Array : undefined!;
const SUint32Array: typeof Uint32Array = Uint32Array;
export { SUint32Array as Uint32Array };
const SencodeURIComponent: typeof encodeURIComponent =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
typeof encodeURIComponent !== 'undefined' ? encodeURIComponent : undefined!;
const SencodeURIComponent: typeof encodeURIComponent = encodeURIComponent;
export { SencodeURIComponent as encodeURIComponent };
const SMap = Map;
export { SMap as Map };
Expand Down
10 changes: 6 additions & 4 deletions website/docs/migration/from-3.x-to-4.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ Simple migration guide to fast-check v4 starting from fast-check v3

## Changes in minimal requirements

| Name | New requirement | Previous requirement |
| ----------------------- | --------------- | -------------------- |
| TypeScript _(optional)_ | ≥5.0 | ≥4.1 |
| Name | New requirement | Previous requirement |
| ------------------------ | --------------- | -------------------- |
| Node | ≥10.5.0 | ≥8 |
| ECMAScript specification | ES2020 | ES2017 |
| TypeScript _(optional)_ | ≥5.0 | ≥4.1 |

Related pull requests: [#5577](https://github.com/dubzzz/fast-check/pull/5577), [#5605](https://github.com/dubzzz/fast-check/pull/5605)
Related pull requests: [#5577](https://github.com/dubzzz/fast-check/pull/5577), [#5605](https://github.com/dubzzz/fast-check/pull/5605), [#5617](https://github.com/dubzzz/fast-check/pull/5617)

## Update to latest v3.x

Expand Down

0 comments on commit 1bf1eb0

Please sign in to comment.