Short: I had some time, so I checked how time-consuming is TesBed

This is going to be short. I cannot stop thinking about my last results from the previous article. I was able to use esbuild instead of ts-jest, and it was still slow.

It looked like using TestBed is time-consuming. So I decided to check it, and I’m ready to show you the results.

The test

For testing, I used the same repository. You can find it on my GitHub: ng-esbuild-unit-tests.

The test is simple. Each it contains a loop and a test. Both tests are testing the same case, tests are checking if the Output of the component emitted a value after calling createItem method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {TestBed} from '@angular/core/testing';

import {CreateFormComponent} from './create-form.component';
import {take} from "rxjs";

const numberOfIterations = 10000

describe('CreateFormComponent', () => {

    it('TestBed - should emit value if value is not empty', async () => {
      for (let i = 0; i < numberOfIterations; i++) {
        await TestBed.configureTestingModule({ declarations: [CreateFormComponent] }).compileComponents();
        const fixture = TestBed.createComponent(CreateFormComponent);
        const component = fixture.componentInstance;
        fixture.detectChanges();

        const value = 'Test';

        const promise = component.itemCreated
          .pipe(take(1))
          .toPromise();

        component.createItem(value);

        const result = await promise;
        expect(result).toEqual(value);
        await TestBed.resetTestingModule();
      }
    });

    it('No TestBed - should emit value if value is not empty', async () => {
      for (let i = 0; i < numberOfIterations; i++) {
        const component = new CreateFormComponent();
        const value = 'Test';
        const promise = component.itemCreated
          .pipe(take(1))
          .toPromise();

        component.createItem(value);

        const result = await promise;
        expect(result).toEqual(value);
      }
    });

});

Results

I decided to run tests a few times. Each time, I increased the number of iterations in the loop inside the tests.

testbedchart

Number of iterationsTestBed [ms]NoTestBed [ms]TestBed time per iteration [ms]No TestBed time per iteration [ms]How many times tests without TestBed were faster that tests with TestBed
104714,70000,100047
10014761,47000,060025
250304131,21600,052023
500523191,04600,038028
1000915320,91500,032029
20001586620,79300,031026
500033081450,66160,029023
1000060282610,60280,026123

Summary

I expected before doing the test that the time of the test will be linear. It’s a simple loop, so it’s natural. What is fascinating is that when I had a small number of iterations, the difference between my two tests was significantly greater. When I had a lot of iterations, the difference was still visible, but the time per iteration was shrinking.

So why not verify it…?

ONE LAST TEST: 100000 ITERATIONS!

Number of iterationsTestBed [ms]NoTestBed [ms]TestBed time per iteration [ms]No TestBed time per iteration [ms]How many times tests without TestBed were faster that tests with TestBed
1000005657337140,56570,037115