Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(trycatch): update benchmarks to return a value between 1k and 10k
- Loading branch information
1 parent
e603879
commit c1095cc
Showing
1 changed file
with
19 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |