Possible performance improvement: Generate messages only if assertions fail
dhardtke opened this issue · 2 comments
Since serializing large objects can sometimes take some time it would be neat if Earl would generate messages lazily, i.e., only if an assertion fails.
For example, instead of:
let's do something like this:
export function toBeTruthy(control: Control) {
control.assert({
success: truthy()(control.actual),
reason: () => `The value ${formatCompact(control.actual)} is not truthy, but it was expected to be truthy.`,
negatedReason: () => `The value ${formatCompact(control.actual)} is truthy, but it was expected not to be truthy.`,
})
}
I do not have benchmarks to back this claim up but I don't see why it wouldn't lead to a substantial performance gain.
I have tested earl on some large project.
It results in 15...20% slower run (comparing with should.js)
Quick testing the @dhardtke idea shows at least 50% better execution time on expect(object).toEqual(...)
CPU snapshot shows that most time is consumed by formatCompact
and preapreStackTrace
.
As I understand, the execution of those functions is only required if test fails. Avoiding them should result in significant performance boost.