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))
7.725316457916051
1.1829904590267688
We noticed that Nested is slower than List of Nested.
Not sure why this happens but should they be the same?
It is couple of times slower: