New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

fast-max

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-max

Quickest Way to get the Maximum Value of an Array of Numbers (Typed or Untyped)

latest
Source
npmnpm
Version
0.5.1
Version published
Maintainers
1
Created
Source

fast-max

:fire: The Quickest Way to get the Maximum Value of an Array of Numbers (Typed or Untyped)

install

npm install fast-max

why is it so much faster?

This library excels with typed arrays. It takes into account the theoretical maximum of a typed array. For example, if you have a Uint8Array, it's not possible for a maximum value to be greater than 255, so if we encounter a 255 in the array, we can stop searching for a higher value.

usage

getting maximum value of a normal array

const fastMax = require("fast-max"); // or import max from "fast-max";

const result = fastMax([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// result is 10

getting maximum value of a typed array

const fastMax = require("fast-max");

const pixel_values = Uint8Array.from([0, 128, 255, 34, ...]);
const result = fastMax(pixel_values);
// result is 255

setting theoretical maximum

If you know that an array's values can't exceed a specific number, you can set the theoretical_max.

const fastMax = require("fast-max");

const numbers = [0, 9, 4, 2, 10, ...]);
const result = fastMax(numbers, { theoretical_max: 10 });
// result is 10

no data value

If you want to ignore one or more specific values, you can set the no_data value.

const fastMax = require("fast-max");

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

Here are test results comparing fast-max to two other popular libraries underscore and lodash. Tests have been conducted by creating an array of ten million random numbers from zero to the maximum theoretical value of the typed array.

array typelibraryaverage duration in milliseconds
Int8Arrayfast-max< 1
Int8Arraylodash20.9
Int8Arrayunderscore14.3
Uint8Arrayfast-max0.1
Uint8Arraylodash21.4
Uint8Arrayunderscore14.3
Int16Arrayfast-max1.4
Int16Arraylodash21
Int16Arrayunderscore13.7
Uint16Arrayfast-max1.9
Uint16Arraylodash20.9
Uint16Arrayunderscore13.8
Int32Arrayfast-max31.2
Int32Arraylodash21.2
Int32Arrayunderscore14.2
Uint32Arrayfast-max109.8
Uint32Arraylodash73.3
Uint32Arrayunderscore15
BigInt64Arrayfast-max115.4
BigInt64Arraylodash237.5
BigInt64Arrayunderscore222.4
BigUint64Arrayfast-max113.9
BigUint64Arraylodash236.4
BigUint64Arrayunderscore219.7

Keywords

array

FAQs

Package last updated on 14 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts