Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Math.max.apply #3

Merged
merged 2 commits into from
Jun 13, 2018
Merged

Conversation

allevo
Copy link
Contributor

@allevo allevo commented Jun 13, 2018

getChild is hot code.

$ node benchMax.js 
Math.max.apply(Math, arr) x 26,094,752 ops/sec ±3.09% (86 runs sampled)
getMax(arr) x 82,660,189 ops/sec ±5.21% (80 runs sampled)
'use strict'

const Benchmark = require('benchmark')
const suite = Benchmark.Suite()

const arr = [1, 5, 2, 7, 3]

function getMax (arr) {
  var l = arr.length
  var max = arr[0]
  for (var i = 1; i < l; i++) {
    if (arr[i] > max) {
      max = arr[i]
    }
  }
  return max
}

suite
  .add('Math.max.apply(Math, arr)', function () {
    Math.max.apply(Math, arr)
  })
  .add('getMax(arr)', function () {
    getMax(arr)
  })
  .on('cycle', function (event) {
    console.log(String(event.target))
  })
  .on('complete', function () {})
  .run()

Copy link
Owner

@delvedor delvedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@delvedor delvedor merged commit 1ea8dac into delvedor:master Jun 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants