Skip to content

Commit

Permalink
Optimize primality check a bit more
Browse files Browse the repository at this point in the history
Add a benchmark to make sure an array inclusion check is beneficial.
  • Loading branch information
frostburn committed Dec 19, 2023
1 parent 68604ca commit 64b7b53
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
"posttest": "npm run lint",
"test": "vitest",
"doc": "typedoc src/index.ts . --name xen-dev-utils",
"prebenchmark": "tsc -p tsconfig-benchmark.json",
"benchmark": "node benchmarks/__benchmarks__/monzo.mark.js"
"premonzo-benchmark": "tsc -p tsconfig-benchmark.json",
"monzo-benchmark": "node benchmarks/__benchmarks__/monzo.mark.js",
"preprimes-benchmark": "tsc -p tsconfig-benchmark.json",
"primes-benchmark": "node benchmarks/__benchmarks__/primes.mark.js"
},
"engines": {
"node": ">=10.6.0"
Expand Down
41 changes: 41 additions & 0 deletions src/__benchmarks__/primes.mark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @ts-ignore
import Benchmark = require('benchmark');
import {PRIMES, isPrime} from '../primes';

function withoutInclusionCheck(n: number) {
if (!Number.isInteger(n) || n < 2) {
return false;
}
if (n > 62837328) {
throw new Error('Prime check only implemented up to 62837328');
}
for (const prime of PRIMES) {
if (prime * prime > n) {
return true;
}
if (n % prime === 0) {
return false;
}
}
return true;
}

function randSmallInt() {
return Math.floor(Math.random() * 10000);
}

const primalityCheckSuite = new Benchmark.Suite();
primalityCheckSuite
.add('primality check (with array inclusion)', () => isPrime(randSmallInt()))
.add('primality check (without array inclusion)', () =>
withoutInclusionCheck(randSmallInt())
)
.on('cycle', (event: {target: any}) => {
console.log(String(event.target));
})
.on('complete', () => {
console.log(
'Fastest is ' + primalityCheckSuite.filter('fastest').map('name')
);
})
.run({async: true});
6 changes: 3 additions & 3 deletions src/primes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ export const BIG_INT_PRIMES = PRIMES.map(BigInt);
* @returns True if the number is prime, false otherwise.
*/
export function isPrime(n: number) {
if (!Number.isInteger(n) || n < 2) {
return false;
}
if (n < 7927) {
return PRIMES.includes(n);
}
if (!Number.isInteger(n)) {
return false;
}
if (n > 62837328) {
throw new Error('Prime check only implemented up to 62837328');
}
Expand Down
6 changes: 5 additions & 1 deletion tsconfig-benchmark.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"extends": "./node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"rootDir": "./src/",
"outDir": "benchmarks"
"outDir": "benchmarks",
"lib": [
"es2020"
],
"target": "es2020"
},
"include": [
"src/__benchmarks__/*"
Expand Down

0 comments on commit 64b7b53

Please sign in to comment.