Skip to content

Commit 613583e

Browse files
committed
Add benchmark item for the unmarshalling
Signed-off-by: moznion <moznion@mail.moznion.net>
1 parent e9dca67 commit 613583e

File tree

6 files changed

+89
-8
lines changed

6 files changed

+89
-8
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,24 @@ This replaces `fromDynamodbRecord<T>(attrs: Record<string, AttributeValue>)` inv
2727

2828
The benchmark result between this project and [kayomarz/dynamodb-data-types](https://github.com/kayomarz/dynamodb-data-types) is the following:
2929

30+
marshalling:
31+
3032
```
3133
node version: v16.17.0
32-
dynamodb-data-types marshalling x 3,475,450 ops/sec ±0.45% (96 runs sampled)
33-
ts-dynamodb-attributes-transformer marshalling x 13,405,409 ops/sec ±0.43% (91 runs sampled)
34+
dynamodb-data-types marshalling x 3,845,247 ops/sec ±0.63% (90 runs sampled)
35+
ts-dynamodb-attributes-transformer marshalling x 13,614,974 ops/sec ±0.24% (100 runs sampled)
3436
Fastest is ts-dynamodb-attributes-transformer marshalling
3537
```
3638

39+
unmarshalling:
40+
41+
```
42+
node version: v16.17.0
43+
dynamodb-data-types unmarshalling x 1,800,718 ops/sec ±0.30% (96 runs sampled)
44+
ts-dynamodb-attributes-transformer unmarshalling x 3,493,272 ops/sec ±0.50% (98 runs sampled)
45+
Fastest is ts-dynamodb-attributes-transformer unmarshalling
46+
```
47+
3748
Please see also [benchmark](./examples/benchmark) project.
3849

3950
## Synopsis

examples/benchmark/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ npm run bench
1313

1414
```
1515
node version: v16.17.0
16-
dynamodb-data-types marshalling x 3,475,450 ops/sec ±0.45% (96 runs sampled)
17-
ts-dynamodb-attributes-transformer marshalling x 13,405,409 ops/sec ±0.43% (91 runs sampled)
16+
dynamodb-data-types marshalling x 3,845,247 ops/sec ±0.63% (90 runs sampled)
17+
ts-dynamodb-attributes-transformer marshalling x 13,614,974 ops/sec ±0.24% (100 runs sampled)
1818
Fastest is ts-dynamodb-attributes-transformer marshalling
19+
dynamodb-data-types unmarshalling x 1,800,718 ops/sec ±0.30% (96 runs sampled)
20+
ts-dynamodb-attributes-transformer unmarshalling x 3,493,272 ops/sec ±0.50% (98 runs sampled)
21+
Fastest is ts-dynamodb-attributes-transformer unmarshalling
1922
```
2023

examples/benchmark/dynamodb_data_types.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,29 @@ function toDynamoDBRecord() {
1515
AttributeValue.wrap(obj);
1616
}
1717

18-
module.exports = toDynamoDBRecord;
18+
function fromDynamoDBRecord() {
19+
AttributeValue.unwrap({
20+
id: {
21+
N: '123455',
22+
},
23+
name: {
24+
S: 'John Doe',
25+
},
26+
tags: {
27+
M: {
28+
foo: {
29+
S: 'bar',
30+
},
31+
buz: {
32+
S: 'qux',
33+
},
34+
},
35+
},
36+
flags: {
37+
SS: ['foo', 'bar'],
38+
},
39+
});
40+
}
41+
42+
module.exports.toDynamoDBRecord = toDynamoDBRecord;
43+
module.exports.fromDynamoDBRecord = fromDynamoDBRecord;

examples/benchmark/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const Benchmark = require('benchmark');
2-
const toDynamoDBRecordByDataTypes = require('./dynamodb_data_types');
2+
const toDynamoDBRecordByDataTypes = require('./dynamodb_data_types').toDynamoDBRecord;
3+
const fromDynamoDBRecordByDataTypes = require('./dynamodb_data_types').fromDynamoDBRecord;
34
const toDynamoDBRecordByTransformer = require('./using_transformer').toDynamoDBRecord;
5+
const fromDynamoDBRecordByTransformer = require('./using_transformer').fromDynamoDBRecord;
46

57
console.log(`node version: ${process.version}`);
68
new Benchmark.Suite()
@@ -16,4 +18,19 @@ new Benchmark.Suite()
1618
.on('complete', function () {
1719
console.log('Fastest is ' + this.filter('fastest').map('name'));
1820
})
19-
.run({ async: true });
21+
.run({ async: false });
22+
23+
new Benchmark.Suite()
24+
.add('dynamodb-data-types unmarshalling', function () {
25+
fromDynamoDBRecordByDataTypes();
26+
})
27+
.add('ts-dynamodb-attributes-transformer unmarshalling', function () {
28+
fromDynamoDBRecordByTransformer();
29+
})
30+
.on('cycle', function (event) {
31+
console.log(String(event.target));
32+
})
33+
.on('complete', function () {
34+
console.log('Fastest is ' + this.filter('fastest').map('name'));
35+
})
36+
.run({ async: false });
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare function toDynamoDBRecord(): void;

examples/benchmark/using_transformer.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dynamodbRecord } from '../../index';
1+
import { dynamodbRecord, fromDynamodbRecord } from '../../index';
22

33
interface Obj {
44
readonly id: number;
@@ -22,3 +22,27 @@ const obj: Obj = {
2222
export function toDynamoDBRecord() {
2323
dynamodbRecord<Obj>(obj);
2424
}
25+
26+
export function fromDynamoDBRecord() {
27+
fromDynamodbRecord<Obj>({
28+
id: {
29+
N: '123455',
30+
},
31+
name: {
32+
S: 'John Doe',
33+
},
34+
tags: {
35+
M: {
36+
foo: {
37+
S: 'bar',
38+
},
39+
buz: {
40+
S: 'qux',
41+
},
42+
},
43+
},
44+
flags: {
45+
SS: ['foo', 'bar'],
46+
},
47+
});
48+
}

0 commit comments

Comments
 (0)