From ada11ea5b46dd740d47b2ad2e481061145691bab Mon Sep 17 00:00:00 2001 From: DanielJDufour Date: Mon, 24 May 2021 18:31:27 -0400 Subject: [PATCH] you can now pass in theoretical_min --- README.md | 48 +++++++++++++++++++++++------------------------ index.js | 20 +++++--------------- package-lock.json | 13 +++++++++++++ package.json | 3 +++ 4 files changed, 45 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index d8cfe09..2fe0d22 100644 --- a/README.md +++ b/README.md @@ -44,27 +44,27 @@ Here are test results comparing fast-min to two other popular libraries undersco Tests have been conducted by creating an array of ten million random numbers from the lowest to the highest theoretical value of the typed array. | array type | library | average duration in milliseconds | | ---------- | ------- | -------------------------------- | -| Int8Array | fast-min | **< 1** | -| Int8Array | lodash | 20.9 | -| Int8Array | underscore | 12.4 | -| Uint8Array | fast-min | **11.3** | -| Uint8Array | lodash | 23.5 | -| Uint8Array | underscore | 12.2 | -| Int16Array | fast-min | **0.5** | -| Int16Array | lodash | 23.6 | -| Int16Array | underscore | 13.3 | -| Uint16Array | fast-min | **13.2** | -| Uint16Array | lodash | 20.5 | -| Uint16Array | underscore | 13.5 | -| Int32Array | fast-min | **15.4** | -| Int32Array | lodash | 20.9 | -| Int32Array | underscore | 12.3 | -| Uint32Array | fast-min | **12.5** | -| Uint32Array | lodash | 60.9 | -| Uint32Array | underscore | 14.4 | -| BigInt64Array | fast-min | **244.2** | -| BigInt64Array | lodash | 257 | -| BigInt64Array | underscore | 245.3 | -| BigUint64Array | fast-min | **200.1** | -| BigUint64Array | lodash | 204.9 | -| BigUint64Array | underscore | 198 | +| Int8Array | fast-min | **0.1** | +| Int8Array | lodash | 20.7 | +| Int8Array | underscore | 14.9 | +| Uint8Array | fast-min | **11.5** | +| Uint8Array | lodash | 22.1 | +| Uint8Array | underscore | 12.6 | +| Int16Array | fast-min | **0.6** | +| Int16Array | lodash | 21.9 | +| Int16Array | underscore | 12.4 | +| Uint16Array | fast-min | **12.2** | +| Uint16Array | lodash | 21.2 | +| Uint16Array | underscore | 12.5 | +| Int32Array | fast-min | **13.1** | +| Int32Array | lodash | 20.6 | +| Int32Array | underscore | 12.6 | +| Uint32Array | fast-min | **12.7** | +| Uint32Array | lodash | 66.6 | +| Uint32Array | underscore | 15.3 | +| BigInt64Array | fast-min | **247.6** | +| BigInt64Array | lodash | 253 | +| BigInt64Array | underscore | 241.6 | +| BigUint64Array | fast-min | **194.5** | +| BigUint64Array | lodash | 206.1 | +| BigUint64Array | underscore | 205.4 | diff --git a/index.js b/index.js index 8156bed..d5e5808 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,11 @@ -const THEORETICAL_MINIMUMS = { - Array: null, - Int8Array: -128, // Math.pow(-2, 8 - 1) - Uint8Array: 0, - Uint8ClampedArray: 0, - Int16Array: -32768, // Math.pow(-2, 16 - 1) - Uint16Array: 0, - Int32Array: -2147483648, // Math.pow(-2, 32 - 1) - Uint32Array: 0, - // skipping Float32Array and Float64Array because it appears to be platform dependent - BigInt64Array: -9223372036854776000, // Math.pow(-2, 63) - BigUint64Array: 0, -}; +const getTheoreticalMin = require("typed-array-ranges/get-min"); module.exports = function fastMin( numbers, - { debug = false, no_data = undefined } = { + { debug = false, no_data = undefined, theoretical_min = undefined } = { debug: false, no_data: undefined, + theoretical_min: undefined, } ) { if (debug) @@ -38,7 +27,8 @@ module.exports = function fastMin( if (debug) console.log("[fast-min] constructor:", numbers.constructor.name); - let theoretical_min = THEORETICAL_MINIMUMS[numbers.constructor.name]; + if (theoretical_min === undefined) + theoretical_min = getTheoreticalMin(numbers.constructor.name); if (debug) console.log("[fast-min] theoretical minimunm is", theoretical_min); if (theoretical_min) { if (no_data !== undefined) { diff --git a/package-lock.json b/package-lock.json index d55b2b3..9051ebc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,9 @@ "": { "version": "0.1.0", "license": "CC0-1.0", + "dependencies": { + "typed-array-ranges": "^0.0.0" + }, "devDependencies": { "flug": "^1.1.0", "lodash.min": "^4.0.1", @@ -25,6 +28,11 @@ "integrity": "sha1-SsG5qLr4ttKKaQ1xZRJRDPwUcIw=", "dev": true }, + "node_modules/typed-array-ranges": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/typed-array-ranges/-/typed-array-ranges-0.0.0.tgz", + "integrity": "sha512-f8IjaA5ELCg0QWdUzep2Ru8CjBHmlDurAwu2L5mOiRdv0KZnpiPnWbZX3fvRHk/AKuglwXV/F7O/tIK3nzLmHQ==" + }, "node_modules/underscore": { "version": "1.13.1", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.1.tgz", @@ -45,6 +53,11 @@ "integrity": "sha1-SsG5qLr4ttKKaQ1xZRJRDPwUcIw=", "dev": true }, + "typed-array-ranges": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/typed-array-ranges/-/typed-array-ranges-0.0.0.tgz", + "integrity": "sha512-f8IjaA5ELCg0QWdUzep2Ru8CjBHmlDurAwu2L5mOiRdv0KZnpiPnWbZX3fvRHk/AKuglwXV/F7O/tIK3nzLmHQ==" + }, "underscore": { "version": "1.13.1", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.1.tgz", diff --git a/package.json b/package.json index 08915cc..266d34e 100644 --- a/package.json +++ b/package.json @@ -41,5 +41,8 @@ "flug": "^1.1.0", "lodash.min": "^4.0.1", "underscore": "^1.13.1" + }, + "dependencies": { + "typed-array-ranges": "^0.0.0" } }