Skip to content

Commit

Permalink
test(trycatch): update benchmarks to return a value between 1k and 10k
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Feb 13, 2017
1 parent e603879 commit c1095cc
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions benchmarks/bm.tryCatch.ts
@@ -1,28 +1,41 @@
/**
* Created by tushar.mathur on 06/11/16.
*/

import * as assert from 'assert'
import {Suite} from 'benchmark'
import {SafeValue} from '../src/lib/SafeValue'
import {toSafeFunction} from '../src/lib/ToSafeFunction'
import {array} from './lib'

function addThis (b: number) {
this.a = this.a + b
if (this.a % 2 === 0) {
throw 'ERROR'
}
return this
}
const safelyAddThis = toSafeFunction(addThis)

function test (message: string, f: Function) {
assert.deepStrictEqual(f().value.a, 102, message)
assert.deepStrictEqual(f(), [
new SafeValue('ERROR'),
new SafeValue({a: 101}),
new SafeValue('ERROR')
], message)
}

export function testFunction (): SafeValue<any> {
return safelyAddThis.call({a: 100}, 2)

export function testFunction (arr: number[]): Array<SafeValue<any>> {
const results = []
for (var i = 0; i < arr.length; ++i) {
results.push(safelyAddThis.call({a: 100}, i))
}
return results
}

test('class-based', testFunction)
test('class-based', () => testFunction([0, 1, 2]))

const arr = array(1e3)
export function bm_tryCatch (suite: Suite) {
return suite.add('tryCatch', testFunction)
return suite.add('tryCatch', () => testFunction(arr))
}

0 comments on commit c1095cc

Please sign in to comment.