chunked-array-vs-native.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import * as B from 'benchmark';
  2. import { ChunkedArray } from '../mol-data/util';
  3. function testNative(size: number) {
  4. const xs = new Array(size);
  5. for (let i = 0; i < size; i++) xs[i] = i * i;
  6. return xs;
  7. }
  8. function testChunkedTyped(size: number, chunk: number) {
  9. const xs = ChunkedArray.create(Int32Array, 1, chunk);
  10. for (let i = 0; i < size; i++) ChunkedArray.add(xs, i * i);
  11. return ChunkedArray.compact(xs);
  12. }
  13. function testChunkedNative(size: number, chunk: number) {
  14. const xs = ChunkedArray.create(Array, 1, chunk);
  15. for (let i = 0; i < size; i++) ChunkedArray.add(xs, i * i);
  16. return ChunkedArray.compact(xs);
  17. }
  18. const suite = new B.Suite();
  19. const N = 70000;
  20. suite
  21. .add('native', () => testNative(N))
  22. // .add('chunkedT 0.1k', () => testChunkedTyped(N, 100, false))
  23. // .add('chunkedT 4k', () => testChunkedTyped(N, 4096, false))
  24. .add('chunkedT 4k lin', () => testChunkedTyped(N, 4096))
  25. // .add('chunkedT N / 2', () => testChunkedTyped(N, N / 2, false))
  26. // .add('chunkedT N', () => testChunkedTyped(N, N, false))
  27. // .add('chunkedT 2 * N', () => testChunkedTyped(N, 2 * N, false))
  28. .add('chunkedN N', () => testChunkedNative(N, N))
  29. .add('chunkedN 0.1k', () => testChunkedNative(N, 100))
  30. .add('chunkedN N / 2', () => testChunkedNative(N, N / 2))
  31. .add('chunkedN 2 * N', () => testChunkedNative(N, 2 * N))
  32. .on('cycle', (e: any) => {
  33. console.log(String(e.target));
  34. })
  35. .run();
  36. // console.log(testChunkedTyped(10, 16));