Skip to content

Commit

Permalink
docs(performance): remove section on literal vs enum performance (#9262)
Browse files Browse the repository at this point in the history
  • Loading branch information
ollz272 committed Apr 19, 2024
1 parent 3c15a8b commit bb857bd
Showing 1 changed file with 0 additions and 29 deletions.
29 changes: 0 additions & 29 deletions docs/concepts/performance.md
Expand Up @@ -140,35 +140,6 @@ class Html(BaseModel):

See [Discriminated Unions] for more details.

## Use `Literal` not `Enum`

Instead of using `Enum`, use `Literal` to define the structure of the data.

??? info "Performance comparison"
With a simple benchmark, `Literal` is about ~3x faster than `Enum`:

```py test="skip"
import enum
from timeit import timeit

from typing_extensions import Literal

from pydantic import TypeAdapter

ta = TypeAdapter(Literal['a', 'b'])
result1 = timeit(lambda: ta.validate_python('a'), number=10000)


class AB(str, enum.Enum):
a = 'a'
b = 'b'


ta = TypeAdapter(AB)
result2 = timeit(lambda: ta.validate_python('a'), number=10000)
print(result2 / result1)
```

## Use `TypedDict` over nested models

Instead of using nested models, use `TypedDict` to define the structure of the data.
Expand Down

0 comments on commit bb857bd

Please sign in to comment.