-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
number.test.js
90 lines (81 loc) · 1.93 KB
/
number.test.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import * as t from './testing.js'
import * as number from './number.js'
import * as random from './random.js'
import * as math from './math.js'
/**
* @param {t.TestCase} _tc
*/
export const testNumber = _tc => {
t.describe('isNaN')
t.assert(number.isNaN(NaN))
t.assert(!number.isNaN(1 / 0))
// @ts-ignore
t.assert(number.isNaN('a' / 0))
t.assert(!number.isNaN(0))
t.describe('isInteger')
t.assert(!number.isInteger(1 / 0))
t.assert(!number.isInteger(NaN))
t.assert(number.isInteger(0))
t.assert(number.isInteger(-1))
t.assert(number.countBits(1) === 1)
t.assert(number.countBits(3) === 2)
t.assert(number.countBits(128 + 3) === 3)
}
/**
* This benchmark confirms performance of division vs shifting numbers.
*
* @param {t.TestCase} tc
*/
export const testShiftVsDivision = tc => {
/**
* @type {Array<number>}
*/
const numbers = []
for (let i = 0; i < 10000; i++) {
numbers.push(random.uint32())
}
t.measureTime('comparison', () => {
for (let i = 0; i < numbers.length; i++) {
let n = numbers[i]
while (n > 0) {
const ns = n >>> 7
const nd = math.floor(n / 128)
t.assert(ns === nd)
n = nd
}
}
})
t.measureTime('shift', () => {
let x = 0
for (let i = 0; i < numbers.length; i++) {
x = numbers[i] >>> 7
}
t.info('' + x)
})
t.measureTime('division', () => {
for (let i = 0; i < numbers.length; i++) {
math.floor(numbers[i] / 128)
}
})
{
/**
* @type {Array<number>}
*/
const divided = []
/**
* @type {Array<number>}
*/
const shifted = []
t.measureTime('division', () => {
for (let i = 0; i < numbers.length; i++) {
divided.push(math.floor(numbers[i] / 128))
}
})
t.measureTime('shift', () => {
for (let i = 0; i < numbers.length; i++) {
shifted.push(numbers[i] >>> 7)
}
})
t.compareArrays(shifted, divided)
}
}