Skip to main content

maxLite

Returns the larger of two numbers using simple comparison. Lightweight alternative to Math.max for comparing exactly two numbers. Uses ternary operator for optimal performance and handles all number types including special values like Infinity and NaN following JavaScript comparison rules.

Signature​

const maxLite: (left: number, right: number) => number

Parameters​

NameTypeDescription
left-First number to compare
right-Second number to compare

Returns​

The larger of the two numbers

Examples​

Basic maximum comparison​

import { maxLite } from '@winglet/common-utils';

console.log(maxLite(5, 3)); // 5
console.log(maxLite(10, 20)); // 20
console.log(maxLite(-1, -5)); // -1 (less negative)
console.log(maxLite(0, -1)); // 0
console.log(maxLite(7, 7)); // 7 (equal values)

Special values and edge cases​

// Special numeric values
console.log(maxLite(1, Infinity)); // Infinity
console.log(maxLite(-Infinity, 5)); // 5
console.log(maxLite(1, NaN)); // NaN (NaN comparisons return false)
console.log(maxLite(NaN, 5)); // 5

// Decimal precision
console.log(maxLite(1.1, 1.2)); // 1.2
console.log(maxLite(0.1 + 0.2, 0.3)); // 0.30000000000000004 (floating point)

Playground​

import { maxLite } from '@winglet/common-utils';

console.log(maxLite(5, 3)); // 5
console.log(maxLite(10, 20)); // 20
console.log(maxLite(-1, -5)); // -1 (less negative)
console.log(maxLite(0, -1)); // 0
console.log(maxLite(7, 7)); // 7 (equal values)

Notes​

Performance Characteristics:

  • Faster than Math.max(a, b) due to no function call overhead
  • Single comparison operation with ternary conditional
  • Ideal for performance-critical code with frequent comparisons
  • No array allocation or iteration required

Use Cases:

  • Performance-critical algorithms requiring frequent pair comparisons
  • Internal utility functions needing minimal overhead
  • Mathematical calculations with bounded values
  • Optimization algorithms (constraint handling, boundary checks)
  • Game development (collision detection, bounds clamping)
  • Data processing pipelines requiring fast comparison operations

Performance: O(1) time and space complexity - single comparison operation.