Skip to content

Commit

Permalink
test(last,assign,isArray,isBoolean,isDate,cluster,isEqual,isError,isI…
Browse files Browse the repository at this point in the history
…nt): add benchmark tests for lodash and radashi
  • Loading branch information
MarlonPassos-git committed Sep 28, 2024
1 parent accb440 commit 153c31b
Showing 1 changed file with 156 additions and 1 deletion.
157 changes: 156 additions & 1 deletion comparisons/comparisons.bench.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as lodash from 'lodash'
import lodash from 'lodash'
import * as radashi from 'radashi'

const libs = { radashi, lodash } as const
Expand Down Expand Up @@ -406,7 +406,162 @@ const benchmarks: Partial<
}
},
},
last: _ => {
const list = [
{ game: 'a', score: 100 },
{ game: 'b', score: 200 },
]
if (isLodash(_)) {
_.last(list)
} else {
_.last(list)
}
},
assign: _ => {
const initial = {
name: 'jay',
cards: ['ac'],
location: {
street: '23 main',
state: {
abbreviation: 'FL',
name: 'Florida',
},
},
}
const override = {
name: 'charles',
cards: ['4c'],
location: {
street: '8114 capo',
state: {
abbreviation: 'TX',
name: 2,
a: 2,
},
},
}
if (isLodash(_)) {
const a = _.assign(initial, override)
} else {
const a = _.assign(initial, override)
}
},
isArray: _ => {
_.isArray([])
},
isBoolean: {
'with boolean value': _ => {
_.isBoolean(true)
},
'with non-boolean value': _ => {
_.isBoolean(null)
},
},
isDate: {
'with valid input': _ => {
_.isDate(new Date())
},
'with invalid input': _ => {
_.isDate(new Date('invalid value'))
},
'with non-Date value': _ => {
_.isDate(22)
},
},
cluster: {
'with default cluster size': _ => {
const list = [1, 1, 1, 1, 1, 1, 1, 1]
if (isLodash(_)) {
_.chunk(list, 2)
} else {
_.cluster(list, 2)
}
},
'specified cluster size of 3': _ => {
const list = [1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2]

if (isLodash(_)) {
_.chunk(list, 3)
} else {
_.cluster(list, 3)
}
},
},
isEqual: {
'with numbers': _ => {
_.isEqual(1, 1)
},
'with string': _ => {
_.isEqual('a', 'a')
},
'with object': _ => {
_.isEqual({ a: 1 }, { a: 1 })
},
'with array': _ => {
_.isEqual([1, 2], [1, 2])
},
'with complex object': _ => {
class Person {
constructor(readonly name: string) {}
}
const jake = new Person('jake')
const symbolKey = Symbol('symbol')
const complex = {
num: 0,
str: '',
boolean: true,
unf: void 0,
nul: null,
obj: { name: 'object', id: 1, children: [0, 1, 2] },
arr: [0, 1, 2],
func() {
console.log('function')
},
loop: null as any,
person: jake,
date: new Date(0),
reg: /\/regexp\/ig/,
[symbolKey]: 'symbol',
}
complex.loop = complex

_.isEqual(complex, { ...complex })
},
},
isError: {
'with error': _ => {
_.isError(new Error())
},
'with non-error': _ => {
_.isError(new Date())
},
},
isInt: {
'with integer': _ => {
if (isLodash(_)) {
_.isInteger(22)
} else {
_.isInt(22)
}
},
'with non-integer': _ => {
if (isLodash(_)) {
_.isInteger(22.0567)
} else {
_.isInt(22.0567)
}
},
'with non-number': _ => {
if (isLodash(_)) {
_.isInteger('abc')
} else {
_.isInt('22')
}
},
},
}

for (const [funcName, run] of Object.entries(benchmarks)) {
if (!radashi.isFunction(run)) {
const tests = Object.entries(run)
Expand Down

0 comments on commit 153c31b

Please sign in to comment.