Skip to content

any.label() significantly slows validation of large arrays #874

@mikejpeters

Description

@mikejpeters

Context

  • node version: 4.3.0
  • joi version: 8.0.5
  • environment: node

What are you trying to achieve or the steps to reproduce?

Use any.label() on a schema within an array, or on the array schema directly.
We use Joi to validate our API responses, so it's common that we are validating large arrays.

const Joi = require('joi');

const users = getUsers();
const schema = Joi.array().items(
  Joi.object().keys({
    username: Joi.string().required()
  })
);

console.time('without label');
Joi.validate(users, schema);
console.timeEnd('without label');

console.time('with label');
Joi.validate(users, schema.label('Users'));
console.timeEnd('with label');

///

function getUsers() {
  const users = [];
  for (var i=0; i<10000; i++) {
    users.push({username: 'example'});
  }
  return users;
}

Which result you had?

A significant impact on validation performance:

without label: 163ms
with label: 448ms

What did you expect?

No noticeable impact on performance.

Metadata

Metadata

Assignees

Labels

breaking changesChange that can breaking existing codefeatureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions