Skip to content

Possible performance improvement: Generate messages only if assertions fail #295

@dhardtke

Description

@dhardtke

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:

https://github.com/l2beat/earl/blob/ea923158550558c46336f0a1038ad522ec62cc43/packages/earl/src/validators/basic/toBeTruthy.ts#L38C1-L45C2

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions