Performance tweak for 1.0
See original GitHub issueSince version 1.0.0-beta.6 I drop the internal type converter into dedicated type converter package TypedConverter. There are reason behind why it should have dedicated type converter VS uses existing converter such as JOI which is mature and fast.
- TypedConverter designed to use TypeScript metadata/reflection so Framework able to convert request information (body, header, params) into appropriate controller parameter type.
- Framework require an “extension point” to do some extra process on each conversion such as Validating the value using existing validator package, or User created validation.
- Joi is a validation library with conversion ability created for JS user, It uses schema configuration vs read from TypeScript reflection.
- Joi validation is synchronous, Plumier provided user to create their own custom validation which require async operation such as access database etc.
Since v1.0.0-beta.6 overall performance dropped about 3% on simple GET request, for complex request with request body it drops even more performance and under express performance point.
Performance Target
Drop performance target should be at most 10% under KOA performance, both for simple GET and complex POST with validation.
Changes
Current conversion logic on TypedConverter will be changed. It will follow how JOI conversion work. It compile Type into optimized AST for conversion, then use the AST to convert the value into appropriate type: example
class Address {
constructor(
public city:string,
public state:string,
public address:string
){}
}
class MyDomain {
constructor(
public name:string,
public birthDate:Date,
public isDecased:boolean,
public address: Address
){}
}
If asked to convert value into above type, TypedConverter will compile above type into AST below:
{
kind: "Object",
type: MyDomain,
properties: [
{
kind: "Primitive",
type: String,
converter: stringConverter,
name: "name"
},
{
kind: "Primitive",
type: Date,
converter: dateConverter,
name: "birthDate"
},
{
kind: "Primitive",
type: Boolean,
converter: booleanConverter,
name: "isDeceased"
},
{
kind: "DeferredObject",
type: Address,
name: "address"
}
}
}
Each properties defined with AST Node with some classification kind
based on its data type. For nested object, the property definition deferred until its ready to convert it will be transformed during conversion using cache to guarantee the speed. This deferred behavior is part of optimization due to value of the property can be null
so its doesn’t waste time during AST transformer.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7
Top GitHub Comments
PR #158 implemented some performance improvement, it successfully reduce framework cost (drop performance caused by framework implementation).
The latest commit of TypedConverter has more performance than Joi (more than twice faster than Joi)