Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
change shallow copy in order to improve perfs (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jul 23, 2016
1 parent af32fbe commit 29020dd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 59 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,11 @@
**Note**: Gaps between patch versions are faulty/broken releases.
**Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.

# 3.2.9

- **Internal**
- change shallow copy in order to improve perfs (@gcanti)

# 3.2.8

- **Bug Fix**
Expand Down
17 changes: 10 additions & 7 deletions lib/update.js
Expand Up @@ -3,18 +3,21 @@ var isObject = require('./isObject');
var isFunction = require('./isFunction');
var isArray = require('./isArray');
var isNumber = require('./isNumber');
var mixin = require('./mixin');

function getShallowCopy(x) {
if (isObject(x)) {
if (x instanceof Date || x instanceof RegExp) {
return x;
}
var ret = {};
for (var k in x) {
ret[k] = x[k];
}
return ret;
}
if (isArray(x)) {
return x.concat();
}
if (x instanceof Date || x instanceof RegExp) {
return x;
}
if (isObject(x)) {
return mixin({}, x);
}
return x;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "tcomb",
"version": "3.2.8",
"version": "3.2.9",
"description": "Type checking and DDD for JavaScript",
"main": "index.js",
"typings": "index.d.ts",
Expand Down
59 changes: 8 additions & 51 deletions perf/perf.js
Expand Up @@ -16,63 +16,20 @@ function onComplete() {
});
}

//
// vanilla
//

function ClassType(name) {
this.name = name;
}

var classInput = 'giulio';
var map = {};

function ClassVersion() {
new ClassType(classInput);
for (var i = 0; i < 10 ; i++) {
map['a' + i] = i;
}

//
// t.struct
//

var structInput = {name: 'giulio'};

var StructType = t.struct({
name: t.String
});
var spec = { a1: { $set: 2 } };

function StructVersion() {
new StructType(structInput);
function updateTest() {
var obj = t.update(map, spec);
obj.a1;
}

Benchmark.Suite({ })
.add('ClassVersion', ClassVersion)
.add('StructVersion', StructVersion)
.add('updateTest', updateTest)
.on('complete', onComplete)
.run({ async: true });

/*
======================
Results (node v0.12.2)
======================
(development)
var t = require('../');
ClassVersion 96726638.34147379 ops/sec
StructVersion 173322.08316550445 ops/sec
---
(development build)
var t = require('../dist/tcomb');
ClassVersion 97459116.74105316 ops/sec
StructVersion 547549.8587466488 ops/sec
---
(production build) `Object.freeze` calls and asserts stripped out
var t = require('../dist/tcomb.min');
ClassVersion 98850069.98176897 ops/sec
StructVersion 2272302.152337918 ops/sec
*/

0 comments on commit 29020dd

Please sign in to comment.