Skip to content

Optimize test efficiency in schema.test.js #15746

@Vegapunk-debug

Description

@Vegapunk-debug

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the performance issue has not already been reported

Last performant version

1.2.5

Slowed down in version

1.x.x

Node.js version

2.0.0

🦥 Performance issue

After upgrading to version 2.x.x, the validation step for schemas has become significantly slower.
A test that previously ran instantly now takes approximately 200–300ms longer.
This slowdown occurs consistently across multiple test runs.

Steps to Reproduce

  1. Install version 2.x.x of the package.
  2. Create a simple schema that includes a Date field.
  3. Run the following test code:
const mongoose = require('mongoose');

const TestSchema = new mongoose.Schema({
  createdAt: Date
});

const Test = mongoose.model('Test', TestSchema);

// Trigger cast error by passing a boolean for a Date field
console.time('validate');
const result = new Test({ createdAt: true }).validateSync();
console.timeEnd('validate');


### Expected Behavior

The validation should run with the same performance as in version 1.x.x.  
Calling `validateSync()` should be fast and should not introduce any noticeable delay.  
There should be no slowdown when validating a schema with a Date field, even when an invalid value (such as a boolean) is provided.

Metadata

Metadata

Assignees

No one assigned

    Labels

    can't reproduceMongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions