-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
211 additions
and
32 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,44 +1,67 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { describe, it } from '@jest/globals'; | ||
import { JsonKit } from '../src'; | ||
import { formatDuration, timer } from './util'; | ||
|
||
describe('[stringify] performance', () => { | ||
it('stringify with ~650KB json data', async () => { | ||
const data = await import('./dataset/ne_110m_populated_places.json'); | ||
const test = (data: any): void => { | ||
const [original, originalDuration] = timer(() => JSON.stringify(data)); | ||
|
||
const original = JSON.stringify(data); | ||
const [basic, basicDuration] = timer(() => JsonKit.stringify(data)); | ||
|
||
const basic = JsonKit.stringify(data); | ||
const [minified, minifiedDuration] = timer(() => | ||
JsonKit.stringify(data, { minify: { key: true } }) | ||
); | ||
|
||
const minified = JsonKit.stringify(data, { minify: { key: true } }); | ||
const [compressed, compressedDuration] = timer(() => | ||
JsonKit.stringify(data, { compress: true }) | ||
); | ||
|
||
expect(basic).toEqual(original); | ||
const [minifiedAndCompressed, minifiedAndCompressedDuration] = timer(() => | ||
JsonKit.stringify(data, { | ||
minify: { key: true }, | ||
compress: true, | ||
}) | ||
); | ||
|
||
console.info( | ||
`${original.length} -> ${minified.length} = ${( | ||
((minified.length - original.length) / original.length) * | ||
100 | ||
).toFixed(2)}%` | ||
); | ||
expect(minified.length).toBeLessThan(original.length); | ||
}); | ||
console.info( | ||
`baseline: ${original.length} (±0.00%) [${formatDuration( | ||
originalDuration | ||
)}]` | ||
); | ||
|
||
it('stringify with ~50MB json data', async () => { | ||
const data = await import('./dataset/ne_10m_roads.json'); | ||
console.info( | ||
`basic: ${basic.length} (±0.00%) [${formatDuration(basicDuration)}]` | ||
); | ||
|
||
const original = JSON.stringify(data); | ||
console.info( | ||
`minify: ${minified.length} (${( | ||
((minified.length - original.length) / original.length) * | ||
100 | ||
).toFixed(2)}%) [${formatDuration(minifiedDuration)}]` | ||
); | ||
|
||
const basic = JsonKit.stringify(data); | ||
console.info( | ||
`compress: ${compressed.length} (${( | ||
((compressed.length - original.length) / original.length) * | ||
100 | ||
).toFixed(2)}%) [${formatDuration(compressedDuration)}]` | ||
); | ||
|
||
const minified = JsonKit.stringify(data, { minify: { key: true } }); | ||
console.info( | ||
`minify + compress: ${minifiedAndCompressed.length} (${( | ||
((minifiedAndCompressed.length - original.length) / original.length) * | ||
100 | ||
).toFixed(2)}%) [${formatDuration(minifiedAndCompressedDuration)}]` | ||
); | ||
}; | ||
|
||
expect(basic).toEqual(original); | ||
describe('[stringify] performance', () => { | ||
it('stringify with ~650KB json data', async () => { | ||
const data = await import('./dataset/ne_110m_populated_places.json'); | ||
test(data); | ||
}); | ||
|
||
console.info( | ||
`${original.length} -> ${minified.length} = ${( | ||
((minified.length - original.length) / original.length) * | ||
100 | ||
).toFixed(2)}%` | ||
); | ||
expect(minified.length).toBeLessThan(original.length); | ||
it('stringify with ~50MB json data', async () => { | ||
const data = await import('./dataset/ne_10m_roads.json'); | ||
test(data); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export function formatDuration(milliseconds: number) { | ||
if (milliseconds < 1000) { | ||
return `${milliseconds.toFixed(0)} ms`; | ||
} | ||
|
||
const seconds = milliseconds / 1000; | ||
if (seconds < 60) { | ||
return `${seconds.toFixed(3)} s`; | ||
} | ||
|
||
const minutes = seconds / 60; | ||
if (minutes < 60) { | ||
return `${minutes.toFixed(2)} m`; | ||
} | ||
|
||
const hours = minutes / 60; | ||
if (hours < 24) { | ||
return `${hours.toFixed(2)} h`; | ||
} | ||
|
||
const days = hours / 24; | ||
return `${days.toFixed(2)} d`; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './format'; | ||
export * from './timer'; |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @param target target function to measure | ||
* @returns Array of [result, durationInMs] | ||
*/ | ||
export function timer<T>(target: () => T): [T, number]; | ||
export async function timer<T extends Promise<any>>( | ||
target: () => T | ||
): Promise<[Awaited<T>, number]>; | ||
export function timer( | ||
target: () => any | ||
): [any, number] | Promise<[any, number]> { | ||
const start = performance.now(); | ||
const result = target(); | ||
if (typeof result?.then === 'function') { | ||
return new Promise((resolve, reject) => { | ||
result | ||
.then((awaited: any) => { | ||
const durationInMs = performance.now() - start; | ||
return resolve([awaited, durationInMs]); | ||
}) | ||
.catch((error: any) => reject(error)); | ||
}); | ||
} else { | ||
const durationInMs = performance.now() - start; | ||
return [result, durationInMs]; | ||
} | ||
} |