marshmallow icon indicating copy to clipboard operation
marshmallow copied to clipboard

Performance impact of Nested with many vs List of Nested.

Open mspiller opened this issue 3 weeks ago • 0 comments

We noticed that Nested is slower than List of Nested. Not sure why this happens but should they be the same?

import timeit

import marshmallow
from marshmallow import fields
from marshmallow import validate


class PropertySchema(marshmallow.Schema):
    name = fields.String(validate=[validate.Regexp('^[0-9a-zA-Z -]{1,100}$')], required=True)
    value = fields.String(validate=[validate.Regexp('^[ -~]{0,255}$')], required=True)


class TrackInteractionActionSchema(marshmallow.Schema):
    properties = fields.Nested(PropertySchema, many=True, required=False)

class TrackInteractionActionSchemaNew(marshmallow.Schema):
    properties = fields.List(fields.Nested(PropertySchema), required=False)


def load_schema(schema: marshmallow.Schema, data: dict) -> None:
    try:
        schema.load(data)
    except validate.ValidationError as err:
        pass


if __name__ == '__main__':
    data = {
        "properties": [
            {"name": "x!0", "value": "v"} for x in range(20000)
        ]
    }

    a = TrackInteractionActionSchema()
    print(timeit.timeit(lambda: load_schema(a, data), number=10))
    b = TrackInteractionActionSchemaNew()
    print(timeit.timeit(lambda: load_schema(b, data), number=10))

It is couple of times slower:

7.725316457916051
1.1829904590267688

mspiller avatar Dec 01 '25 20:12 mspiller