Skip to content

Feature: allow to pass tasks to Bench constructor #268

Description

@fisker

For simple cases,

Instead of

const bench = new Bench({ name: 'simple benchmark', time: 100 })

bench
  .add('faster task', () => {
    console.log('I am faster')
  })
  .add('slower task', async () => {
    await new Promise(resolve => setTimeout(resolve, 1)) // we wait 1ms :)
    console.log('I am slower')
  })

await bench.run()

Use

const bench = new Bench({ 
  name: 'simple benchmark',
  time: 100,
  tasks: [
    {
       name: 'faster task',
       fn: () => {
         console.log('I am faster')
       }),
    },
    {
       name: 'slower task',
       fn: async () => {
         await new Promise(resolve => setTimeout(resolve, 1)) // we wait 1ms :)
         console.log('I am faster')
       }),
    }
  ]
})

await bench.run()

Maybe we can also support pass functions directly, use the function name as name

const bench = new Bench({ 
  name: 'simple benchmark',
  time: 100,
  tasks: [
    function a(){},
    function b(){},
  ]
})

await bench.run()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions