Sum Odd Numbers (v4)

Revision 4 of this benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Base
function sumOddNumbers(nums) {
  let sum = 0;
  for (let i = 0; i < nums.length; i++) {
    const num = nums[i];
    if (Number.isInteger(num) && num % 2 !== 0) sum += num;
  }
}

sumOddNumbers([-1, -2, -3, 4, 5]);
ready
Abs Modulo
function sumOddNumbers(nums) {
  let sum = 0;
  for (let i = 0; i < nums.length; i++) {
    const num = nums[i];
    if (Number.isInteger(num) && Math.abs(num % 2) === 1) sum += num;
  }
}

sumOddNumbers([-1, -2, -3, 4, 5]);
ready
Reduce
function sumOddNumbers(nums) {
  return nums.reduce(
    (sum, num) => (Number.isInteger(num) && num % 2 !== 0 ? sum + num : sum),
    0
  );
}

sumOddNumbers([-1, -2, -3, 4, 5]);
ready
Abs Modulo Reduce
function sumOddNumbers(nums) {
  return nums.reduce(
    (sum, num) =>
      Number.isInteger(num) && Math.abs(num % 2) === 1 ? sum + num : sum,
    0
  );
}

sumOddNumbers([-1, -2, -3, 4, 5]);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.