
## The Performance of Fac

Fac has a good performance to meet the development needs of most applications. For general application development, Fac is even faster than class. In creating a lot of children, Fac is faster than Object.assign. 

Before running the tests of benchmark, clone the Fac repo first (if you have not done it yet):

```bash
$ git clone https://github.com/tasjs/fac.git
$ cd fac
$ npm install
```

　

### Speed of APIs

Run the following:

```bash
$ cd /path/to/fac
$ node benchmark/api.js
```

The results will be like below ():

```bash
Platform info:
macOS Sierra 10.12 x64
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz x 4
Total Memory 16 GB
Node.js v6.11.3
V8 5.1.281.107
--------------
ext                  2042904    times/sec (100000 times, 53 ms)
extends              127570     times/sec (100000 times, 798 ms)
new                  188807     times/sec (100000 times, 541 ms)
spawn                2789507    times/sec (100000 times, 42 ms)
```

　


### Comparison of general inheritance

Run the following:

```bash
$ cd /path/to/fac
$ node benchmark/general-inheritance.js
```

The results will be like below:

```bash
Platform info:
macOS Sierra 10.12 x64
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz x 4
Total Memory 16 GB
Node.js v6.11.3
V8 5.1.281.107
--------------
fac                  30452    times/sec (100000 times, 3312 ms)  <= winner
native-class         12268    times/sec (100000 times, 8204 ms)
native-inheritance   5047     times/sec (100000 times, 20133 ms)
```

　

### Comparison of depth inheritance (20 levels)

Run the following:

```bash
$ cd /path/to/fac
$ node benchmark/depth-inheritance-20-levels.js
```

The results will be like below:

```bash
Platform info:
macOS Sierra 10.12 x64
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz x 4
Total Memory 16 GB
Node.js v6.11.3
V8 5.1.281.107
--------------
fac                  10111    times/sec (100000 times, 9910 ms)  <= winner
native-class         7444     times/sec (100000 times, 13463 ms)
native-inheritance   2526     times/sec (100000 times, 39635 ms)
```

　

### Comparison of depth inheritance (30 levels)

Run the following:

```bash
$ cd /path/to/fac
$ node benchmark/__bad/depth-inheritance-30-levels.js
```

The results will be like below:

```bash
Platform info:
macOS Sierra 10.12 x64
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz x 4
Total Memory 16 GB
Node.js v6.11.3
V8 5.1.281.107
--------------
fac                  2785	times/sec (100000 times, 36347 ms) <= good
native-class         4721	times/sec (100000 times, 21379 ms)
native-inheritance   1465	times/sec (100000 times, 69673 ms)
```

　

### Comparison of creating a lot of complex children

Run the following:

```bash
$ cd /path/to/fac
$ node benchmark/__bad/create-a-lot-of-complex-children.js
```

The results will be like below:

```bash
Platform info:
macOS Sierra 10.12 x64
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz x 4
Total Memory 16 GB
Node.js v6.11.3
V8 5.1.281.107
--------------
fac.new                   186383     times/sec (100000 times, 547 ms) <= good
fac.spawn                 2197404    times/sec (100000 times, 51 ms)  <= winner
native-class              1889187    times/sec (100000 times, 63 ms)
native-inheritance        1898484    times/sec (100000 times, 60 ms)
object.assign             1450592    times/sec (100000 times, 72 ms)
```

　

### Comparison of creating a lot of simple children

Run the following:

```bash
$ cd /path/to/fac
$ node benchmark/__bad/create-a-lot-of-simple-children.js
```

The results will be like below:

```bash
Platform info:
macOS Sierra 10.12 x64
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz x 4
Total Memory 16 GB
Node.js v6.11.3
V8 5.1.281.107
--------------
fac.new                   495054     times/sec (100000 times, 210 ms) <= good
fac.spawn                 2264622    times/sec (100000 times, 52 ms)  <= perfect
native-class              4982158    times/sec (100000 times, 26 ms)
native-inheritance        3667773    times/sec (100000 times, 34 ms)
object.assign             1485701    times/sec (100000 times, 72 ms)
```

　

### Conclusion

Now we can say that Fac has a good performance to meet the development needs of most applications. In some extreme cases, Fac is slower than class; but considering that Fac is easier to use than class, and has excellent features that class does not have, and this "slowness" is relative, so it is reasonable.

　

### Extreme Cases

If you wanna create an inheritance hierarchy of more than 30 levels, wow,  consider the following:

-- If your purpose is "just enough", Fac is your first choice. [See Example](./benchmark/__bad/depth-inheritance-30-levels/fac.js)

-- If you care about the speed extremely, the class is your first choice.

　

If you wanna create a lot of complex child objects with many properties and methods in a very short time, e.g., 100K times/sec, consider the following:

-- If the child objects do not use arrays or objects, or they can share the array data or object data, use fac.spawn (like Object.assign, but faster). [See Example](./benchmark/__bad/create-a-lot-of-complex-children/fac.spawn.js)

-- If the child objects do use arrays or objects, and they can not share the array data or object data, use class or classical inheritance.

　

### About Validity

Yes, the above tests looks so good. But how can we prove that the results are correct? Well, just run some of the above test scripts independently, like below:

```bash
$ cd /path/to/fac
$ node benchmark/general-inheritance/fac.js
```

Because the test script is running independently, it is in the debug state and outputs logs like the following, so that we can easily confirm the result is correct via compare the result and the source code.

```bash
Animal init done... Tobe
Mammal init done... Tobe
Dog init done... Tobe
----------------------------
Dog 3 male white
Dog [ 'white', 'black', 'gray' ]
Dog { body: 1, mouth: 1, eye: 2, foot: 4 }
Hi from Dog 3
Dog 3 male running...
----------------------------
Tobe 4 boy black
Tobe [ 'white', 'black', 'gray', 'yellow' ]
Tobe { body: 1, mouth: 1, eye: 2, foot: 4, xx: 'xx' }
Hi from Tobe 4
Tobe 4 boy running...
----------------------------
true
true
true
true
true
true
true
true
```

　

### About Benchmark

The above tests are based on [OYO](https://github.com/hiowenluke/oyo), it's a benchmark for concurrent performance of Node.js app, strongly recommend.
