Skip to content

Commit

Permalink
added support for multiple no data values and automatic skipping of NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Jan 3, 2024
1 parent c94175d commit 933f53a
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 78 deletions.
53 changes: 28 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ const result = fastMax(numbers, { theoretical_max: 10 });
```

# no data value
If you want to ignore a specific value, you can set the no_data value.
If you want to ignore one or more specific values, you can set the no_data value.
```javascript
const fastMax = require("fast-max");

const numbers = [99, 0, 7, 99, 5, ...]);
const numbers = [99, 0, 7, 99, 5, ...];
const result = fastMax(numbers, { no_data: 99 });
// result is 7

const result = fastMax(numbers, { no_data: [7, 99] });
// result is still 5
```

# performance tests
Expand All @@ -56,27 +59,27 @@ Tests have been conducted by creating an array of ten million random numbers fro
theoretical value of the typed array.
| array type | library | average duration in milliseconds |
| ---------- | ------- | -------------------------------- |
| Int8Array | fast-max | **< 1** |
| Int8Array | lodash | 20.5 |
| Int8Array | underscore | 12.4 |
| Int8Array | fast-max | **0.1** |
| Int8Array | lodash | 21.1 |
| Int8Array | underscore | 9.4 |
| Uint8Array | fast-max | **0.1** |
| Uint8Array | lodash | 24.9 |
| Uint8Array | underscore | 12.5 |
| Int16Array | fast-max | **0.8** |
| Int16Array | lodash | 20.5 |
| Int16Array | underscore | 12.4 |
| Uint16Array | fast-max | **1.1** |
| Uint16Array | lodash | 21.9 |
| Uint16Array | underscore | 12.5 |
| Int32Array | fast-max | **13.2** |
| Int32Array | lodash | 20.6 |
| Int32Array | underscore | 12.4 |
| Uint32Array | fast-max | **13.5** |
| Uint32Array | lodash | 66.6 |
| Uint32Array | underscore | 14.3 |
| BigInt64Array | fast-max | **200.7** |
| BigInt64Array | lodash | 214.1 |
| BigInt64Array | underscore | 207.5 |
| BigUint64Array | fast-max | **199.2** |
| BigUint64Array | lodash | 212 |
| BigUint64Array | underscore | 202.9 |
| Uint8Array | lodash | 20.8 |
| Uint8Array | underscore | 9.4 |
| Int16Array | fast-max | **0.9** |
| Int16Array | lodash | 20.8 |
| Int16Array | underscore | 9.3 |
| Uint16Array | fast-max | **2.8** |
| Uint16Array | lodash | 20.8 |
| Uint16Array | underscore | 9.8 |
| Int32Array | fast-max | **51.5** |
| Int32Array | lodash | 21.6 |
| Int32Array | underscore | 9.6 |
| Uint32Array | fast-max | **114.3** |
| Uint32Array | lodash | 23.4 |
| Uint32Array | underscore | 13.3 |
| BigInt64Array | fast-max | **53.1** |
| BigInt64Array | lodash | 83.5 |
| BigInt64Array | underscore | 64.8 |
| BigUint64Array | fast-max | **50.9** |
| BigUint64Array | lodash | 110.1 |
| BigUint64Array | underscore | 98.5 |
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function fastMax(
numbers: ARRAY_TYPE,
options?: {
debug?: boolean | undefined;
no_data?: number | undefined;
no_data?: number[] | number | undefined;
theoretical_max?: number | undefined;
}
): number;
47 changes: 14 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function fastMax(
{ debug = false, no_data = undefined, theoretical_max = undefined } = {
debug: false,
no_data: undefined,
theoretical_max: undefined,
theoretical_max: undefined
}
) {
if (debug) console.log("[fast-max] starting with numbers:", numbers.slice(0, 10));
Expand All @@ -16,6 +16,8 @@ function fastMax(
}
if (numbers.length === 0) throw new Error("[fast-max] You passed in an empty array");

if (Array.isArray(no_data) === false) no_data = [no_data];

let max;
const length = numbers.length;

Expand All @@ -25,50 +27,29 @@ function fastMax(

if (debug) console.log("[fast-max] theoretical maximunm is", theoretical_max);
if (theoretical_max) {
if (no_data !== undefined) {
max = -Infinity;
for (let i = 1; i < length; i++) {
const value = numbers[i];
if (value > max && value !== no_data) {
max = value;
if (value >= theoretical_max) {
if (debug) console.log("[fast-max] found maximum value of " + value + " at index " + i + " of " + length);
break;
}
}
}
if (max === -Infinity) max = undefined;
} else {
max = numbers[0];
for (let i = 1; i < length; i++) {
const value = numbers[i];
if (value > max) {
max = value;
if (value >= theoretical_max) {
if (debug) console.log("[fast-max] found maximum value of " + value + " at index " + i + " of " + length);
break;
}
max = -Infinity;
for (let i = 1; i < length; i++) {
const value = numbers[i];
// value !== value is a quicker way to do !isNaN(value)
if (typeof value === "number" && value === value && no_data.indexOf(value) === -1 && value > max) {
max = value;
if (value >= theoretical_max) {
if (debug) console.log("[fast-max] found maximum value of " + value + " at index " + i + " of " + length);
break;
}
}
}
if (max === -Infinity) max = undefined;
} else {
if (no_data !== undefined) {
max = -Infinity;
for (let i = 0; i < length; i++) {
const value = numbers[i];
if (value > max && value !== no_data) {
if (typeof value === "number" && value === value && no_data.indexOf(value) === -1 && value > max) {
max = value;
}
}
if (max === -Infinity) max = undefined;
} else {
max = numbers[0];
for (let i = 1; i < length; i++) {
const value = numbers[i];
if (value > max) {
max = value;
}
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"scripts": {
"f": "npm run format",
"format": "npx prettier --arrow-parens=avoid --print-width=120 --write *.js *.ts",
"format": "npx prettier --arrow-parens=avoid --print-width=120 --trailing-comma=none --write *.js *.ts",
"setup": "node setup.js",
"perf": "./perf",
"test": "npm run test:js && npm run test:ts",
Expand Down Expand Up @@ -43,9 +43,9 @@
},
"homepage": "https://github.com/DanielJDufour/fast-max#readme",
"devDependencies": {
"flug": "^2.3.1",
"flug": "^2.7.1",
"lodash.max": "^4.0.1",
"underscore": "^1.13.4"
"underscore": "^1.13.6"
},
"dependencies": {
"typed-array-ranges": "^0.0.0"
Expand Down
4 changes: 2 additions & 2 deletions perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const [_node, _perfjs, array_type, max_lib] = process.argv;
const MAX_FUNCS = {
lodash: require("lodash.max"),
underscore: require("underscore").max,
"fast-max": require("./index"),
"fast-max": require("./index")
};

const ARRAY_CONSTRUCTORS = {
Expand All @@ -17,7 +17,7 @@ const ARRAY_CONSTRUCTORS = {
Int32: Int32Array,
Uint32: Uint32Array,
BigInt64: BigInt64Array,
BigUint64: BigUint64Array,
BigUint64: BigUint64Array
};

const numbers = ARRAY_CONSTRUCTORS[array_type].from(
Expand Down
2 changes: 1 addition & 1 deletion setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const getRandomNums = (nbits, signed) => {
{ name: "int32", bits: 32, signed: true },
{ name: "uint32", bits: 32, signed: false },
{ name: "bigint64", bits: 64, signed: true },
{ name: "biguint64", bits: 64, signed: false },
{ name: "biguint64", bits: 64, signed: false }
].forEach(({ name, bits, signed }) => {
const filename = name + "-numbers.json";
fs.writeFileSync(filename, JSON.stringify(getRandomNums(bits, signed)), "utf-8");
Expand Down
34 changes: 23 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
const fs = require("fs");
const test = require("flug");
const max = require("./index");
const fastMax = require("./index");

test("gettings maximum from a normal array", ({ eq }) => {
const numbers = [920, 550, 340, 690, 550, 340, 840, 700, 550, 210, 540];
const result = max(numbers, { debug: false });
const result = fastMax(numbers, { debug: false });
eq(result, 920);
});

test("getting maximum from an array of image band values", ({ eq }) => {
const numbers = Uint8Array.from(JSON.parse(fs.readFileSync("uint8-numbers.json", "utf-8")));
console.log("loaded uint8 numbers of length:", numbers.length);
const result = max(numbers, { debug: false });
const result = fastMax(numbers, { debug: false });
eq(result, 255);
});

test("setting theoretical maximum", ({ eq }) => {
const numbers = Array.from(Uint8Array.from(JSON.parse(fs.readFileSync("uint8-numbers.json", "utf-8"))));
console.log("loaded uint8 numbers of length:", numbers.length);
const result = max(numbers, { debug: false, theoretical_max: 255 });
const result = fastMax(numbers, { debug: false, theoretical_max: 255 });
eq(result, 255);
});

Expand All @@ -27,41 +27,53 @@ test("getting maximum from typed arrays", ({ eq }) => {
[Int8Array, 127],
[Uint8Array, 255],
[Int16Array, 32767],
[Uint16Array, 65535],
[Uint16Array, 65535]
].forEach(([array_type, expected_max]) => {
const filename = array_type.name.replace("Array", "").toLowerCase() + "-numbers.json";
const numbers = array_type.from(JSON.parse(fs.readFileSync(filename, "utf-8")));
const result = max(numbers, { debug: false });
const result = fastMax(numbers, { debug: false });
eq(result, expected_max);
});
});

test("getting maximum from a very large untyped array", ({ eq }) => {
const numbers = JSON.parse(fs.readFileSync("uint16-numbers.json", "utf-8")).map(n => Number(n));
const result = max(numbers, { debug: true });
const result = fastMax(numbers, { debug: true });
eq(result, 65535);
});

test("getting no maximum from normal arrays with all no data values", ({ eq }) => {
const numbers = [99, 99, 99, 99];
const result = max(numbers, { no_data: 99 });
const result = fastMax(numbers, { no_data: 99 });
eq(result, undefined);
});

test("getting maximum from normal arrays with some data values", ({ eq }) => {
const numbers = [1, 99, 2, 99, 4, 99, 6, 99, -10];
const result = max(numbers, { no_data: 99 });
const result = fastMax(numbers, { no_data: 99 });
eq(result, 6);
});

test("getting no maximum from typed arrays with all no data values", ({ eq }) => {
const numbers = Uint8Array.from([99, 99, 99, 99]);
const result = max(numbers, { no_data: 99 });
const result = fastMax(numbers, { no_data: 99 });
eq(result, undefined);
});

test("getting maximum from typed arrays with some data values", ({ eq }) => {
const numbers = Int8Array.from([1, 99, 2, 99, 4, 99, 6, 99, -10]);
const result = max(numbers, { no_data: 99 });
const result = fastMax(numbers, { no_data: 99 });
eq(result, 6);
});

test("multiple no data values", ({ eq }) => {
const numbers = [99, 0, 7, 99, 5];
const result = fastMax(numbers, { no_data: [7, 99] });
eq(result, 5);
});

test("multiple no data and invalid values", ({ eq }) => {
const numbers = [1, null, 99, 2, 99, undefined, 4, 99, 6, 99, -10, NaN];
const result = fastMax(numbers, { no_data: [-10, 99] });
eq(result, 6);
});
10 changes: 8 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test("getting maximum from typed arrays", ({ eq }) => {
[Int8Array, 127] as const,
[Uint8Array, 255] as const,
[Int16Array, 32767] as const,
[Uint16Array, 65535] as const,
[Uint16Array, 65535] as const
].forEach(([array_type, expected_max]) => {
const filename = array_type.name.replace("Array", "").toLowerCase() + "-numbers.json";
const numbers = array_type.from(JSON.parse(readFileSync(filename, "utf-8")));
Expand All @@ -37,7 +37,7 @@ test("getting maximum from typed arrays", ({ eq }) => {
});

test("getting maximum from a very large untyped array", ({ eq }) => {
const numbers = JSON.parse(readFileSync("uint16-numbers.json", "utf-8")).map(n => Number(n));
const numbers = JSON.parse(readFileSync("uint16-numbers.json", "utf-8")).map((n: string) => Number(n));
const result = max(numbers, { debug: true });
eq(result, 65535);
});
Expand Down Expand Up @@ -65,3 +65,9 @@ test("getting maximum from typed arrays with some data values", ({ eq }) => {
const result = max(numbers, { no_data: 99 });
eq(result, 6);
});

test("multiple no data and invalid values", ({ eq }) => {
const numbers = [1, null, 99, 2, 99, undefined, 4, 99, 6, 99, -10, NaN] as number[];
const result = max(numbers, { no_data: [-10, 99] });
eq(result, 6);
});

0 comments on commit 933f53a

Please sign in to comment.