diff --git a/comparisons/comparisons.bench.ts b/comparisons/comparisons.bench.ts index 913399c5..dc790e6a 100644 --- a/comparisons/comparisons.bench.ts +++ b/comparisons/comparisons.bench.ts @@ -35,8 +35,6 @@ const objects: any = radashi.list(0, 5, i => { const array100 = new Array(100) -const array100 = new Array(100) - const benchmarks: Partial< Record> > = { @@ -81,33 +79,33 @@ const benchmarks: Partial< _.group(list, x => x.group) } }, - // pick: { - // broken - // 'with empty keys': _ => { - // const input = { a: 2 } - // if (isLodash(_)) { - // _.pickBy(input, []) - // } else { - // _.pick(input, []) - // } - // }, - // 'with key not in object': _ => { - // const input = { a: 2, b: 3 } - // if (isLodash(_)) { - // _.pickBy(input, ['c']) - // } else { - // _.pick(input, ['c'] as unknown as ['b']) - // } - // }, - // 'with one key not in object': _ => { - // const input = { a: 2, b: 3 } - // if (isLodash(_)) { - // _.pickBy(input, ['a', 'c']) - // } else { - // _.pick(input, ['a', 'c'] as unknown as ['a']) - // } - // }, - // }, + pick: { + // broken + 'with empty keys': _ => { + const input = { a: 2 } + if (isLodash(_)) { + _.pickBy(input, []) + } else { + _.pick(input, []) + } + }, + 'with key not in object': _ => { + const input = { a: 2, b: 3 } + if (isLodash(_)) { + _.pickBy(input, ['c']) + } else { + _.pick(input, ['c'] as unknown as ['b']) + } + }, + 'with one key not in object': _ => { + const input = { a: 2, b: 3 } + if (isLodash(_)) { + _.pickBy(input, ['a', 'c']) + } else { + _.pick(input, ['a', 'c'] as unknown as ['a']) + } + }, + }, castArray: { // ok // ok @@ -330,228 +328,228 @@ const benchmarks: Partial< _.isEmpty('abc') }, }, - // snake: { - // 'with valid input': _ => { - // const input = 'hello world' - // if (isLodash(_)) { - // _.snakeCase(input) - // } else { - // _.snake(input) - // } - // }, - // }, - // title: _ => { - // if (isLodash(_)) { - // _.startCase('hello world') - // } else { - // _.title('hello world') - // } - // }, - // dash: _ => { - // if (isLodash(_)) { - // _.kebabCase('hello world') - // } else { - // _.dash('hello world') - // } - // }, - // camel: _ => { - // if (isLodash(_)) { - // _.camelCase('hello world') - // } else { - // _.camel('hello world') - // } - // }, - // cloneDeep: _ => { - // if (isLodash(_)) { - // _.cloneDeep(objects) - // } else { - // _.cloneDeep(objects) - // } - // }, - // flat: _ => { - // const lists = [['a', 'b'], ['c', 'd'], ['e']] - // if (isLodash(_)) { - // _.flatten(lists) - // } else { - // _.flat(lists) - // } - // }, - // diff: { - // 'with entirely different arrays': _ => { - // const list1 = ['a', 'b', 'c'] - // const list2 = ['c', 'd', 'e'] - // if (isLodash(_)) { - // _.difference(list1, list2) - // } else { - // _.diff(list1, list2) - // } - // }, - // 'with identity function': _ => { - // const identity = ({ letter }: { letter: string }) => letter - // const letter = (l: string) => ({ letter: l }) - // const list1 = [letter('a'), letter('b'), letter('c')] - // const list2 = [letter('c'), letter('d'), letter('e')] - // if (isLodash(_)) { - // _.differenceWith(list1, list2, (a, b) => identity(a) === identity(b)) - // } else { - // _.diff(list1, list2, identity) - // } - // }, - // }, - // 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] + snake: { + 'with valid input': _ => { + const input = 'hello world' + if (isLodash(_)) { + _.snakeCase(input) + } else { + _.snake(input) + } + }, + }, + title: _ => { + if (isLodash(_)) { + _.startCase('hello world') + } else { + _.title('hello world') + } + }, + dash: _ => { + if (isLodash(_)) { + _.kebabCase('hello world') + } else { + _.dash('hello world') + } + }, + camel: _ => { + if (isLodash(_)) { + _.camelCase('hello world') + } else { + _.camel('hello world') + } + }, + cloneDeep: _ => { + if (isLodash(_)) { + _.cloneDeep(objects) + } else { + _.cloneDeep(objects) + } + }, + flat: _ => { + const lists = [['a', 'b'], ['c', 'd'], ['e']] + if (isLodash(_)) { + _.flatten(lists) + } else { + _.flat(lists) + } + }, + diff: { + 'with entirely different arrays': _ => { + const list1 = ['a', 'b', 'c'] + const list2 = ['c', 'd', 'e'] + if (isLodash(_)) { + _.difference(list1, list2) + } else { + _.diff(list1, list2) + } + }, + 'with identity function': _ => { + const identity = ({ letter }: { letter: string }) => letter + const letter = (l: string) => ({ letter: l }) + const list1 = [letter('a'), letter('b'), letter('c')] + const list2 = [letter('c'), letter('d'), letter('e')] + if (isLodash(_)) { + _.differenceWith(list1, list2, (a, b) => identity(a) === identity(b)) + } else { + _.diff(list1, list2, identity) + } + }, + }, + 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 + 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') - // } - // }, - // }, + _.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)) { diff --git a/vitest.config.ts b/vitest.config.ts index 31275995..840033bc 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,5 +1,3 @@ -import fs from 'node:fs' -import v8 from 'node:v8' import { defineConfig } from 'vitest/config' const resolve = (specifier: string) => @@ -11,22 +9,6 @@ export default defineConfig(env => ({ include: ['tests/**/*.test.ts'], benchmark: { include: ['(benchmarks|comparisons)/**/*.bench.ts'], - reporters: [ - 'default', - { - onInit() { - // debug heap stats - ;(async () => { - while (true) { - await new Promise(r => setTimeout(r, 1000)) - const stats = v8.getHeapStatistics() - stats.malloced_memory - fs.appendFileSync('./heap.csv', `${stats.used_heap_size}\n`) - } - })() - }, - }, - ], }, coverage: { thresholds: { 100: true },