Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extremely long validation time for complex object #39

Closed
bzuillsmith opened this issue May 9, 2014 · 5 comments
Closed

Extremely long validation time for complex object #39

bzuillsmith opened this issue May 9, 2014 · 5 comments
Labels

Comments

@bzuillsmith
Copy link

Okay, if I validate this object it takes <1 ms.

{
    firstname: 'First',
    lastname: 'Last',
}

If I validate this object, it takes 32ms. What is happening that the ObjectID adds 31 ms processing time? Processing time goes up linearly, the more ObjectID's it has to check. Sometimes I need to check arrays of objects with ObjectIDs and that gets outrageous.

{
    _id: new ObjectID(),  // mongodb id
    firstname: 'First',
    lastname: 'Last',
}
@zaggino zaggino added the bug label May 12, 2014
@zaggino
Copy link
Owner

zaggino commented May 12, 2014

There's of course something very wrong going on in there - I'll look for a fix and let you know.

@zaggino
Copy link
Owner

zaggino commented Jun 15, 2014

@bzuillsmith can you give me a test-case with a schema you're testing against? Seems like inner properties of ObjectID() are being validated with your schema and that's causing the delay but I can't say for sure until I see the schema.

@bzuillsmith
Copy link
Author

Below is an example test based on a subset of one of our use cases. Usually we add format: 'object-id that checks it's an instance of ObjectID but seems to make no difference to the problem.

On my machine, async validation takes 232ms and sync takes 200ms.

var ObjectID = require('mongodb').ObjectID;
var ZSchema = require('z-schema');

describe('z-schema', function() {
    var schema = {
        type: 'array',
        items: {
            type: 'object',
            properties: {
                'note': { type: 'string', maxLength: 2048 },
                // 'created':  { type: 'object', format: 'date-object' },
                'createdBy': { type: 'string', maxLength: 100 },
                '_id': { type: 'object' }
            },
            additionalProperties: false,
        },
        additionalItems: false,
    };

    var notes = [];

    before(function() {
        var num = 5, i;
        for(i = 0; i < num; i++) {
            notes.push({
                _id: new ObjectID(),
                note: 'Here is my note',
                createdBy: 'test@whatever.com',
            });
        }
        return notes;
    });

    it('should be fast', function(done) {
        var validator = new ZSchema({ async: true, noTypeless: true, noExtraKeywords: true, noZeroLengthStrings: true, forceAdditional: true});
        validator.validate(notes, schema, done);
    });
});

zaggino added a commit that referenced this issue Jun 15, 2014
@zaggino
Copy link
Owner

zaggino commented Jun 15, 2014

@bzuillsmith here's the testcase I put together: ac4ed8c

I didn't notice any major difference with using ObjectID against using simple object. Btw on my computer validation over 10000 objects (I increased from your original five) took about 250ms in sync mode and 400ms in async mode.

Can you please try to run the testcase on your machine? Just clone the repo and do

npm install
npm install -g mocha
mocha --grep "issue/39"

@zaggino
Copy link
Owner

zaggino commented Jun 16, 2014

Confirmed solved in ac4ed8c#commitcomment-6674756

@zaggino zaggino closed this as completed Jul 26, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants