-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_utils_optimize.js
92 lines (78 loc) · 1.95 KB
/
math_utils_optimize.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
91
92
// @ts-check
/**
* @file With various utility and helper functionality
* @author Jonas Brandel
*/
/** lib_version = Current version of this file
* @constant
@type {number}
@default
*/
const lib_version = 1;
/**
* Function that tells if a integer is an even number
* @example
* // returns true
* isEven(22);
* @param {number} number - An integer number to check if number is even
*/
var isEven = function (number){
// It would be enough to check if least significant bit is 0 to mean
// it is divisable by 2
// but doubt it would make a difference
// var lastDigit = number & 1;
// return !lastDigit;
return number % 2 == 0;
}
console.log(isEven(148));
/**
* Function that tells if a integer is an odd number
* @example
* // returns false
* isEven(22);
* @param {number} number - An integer number to check if number is odd
*/
function isOdd (number){
//TODO - This should use an expression instead of invoking a function, which has a higher cost!
return !isEven(number);
}
var isEvenFast = function (number){
// It would be enough to check if least significant bit is 0 to mean
// it is divisable by 2
// but doubt it would make a difference
return !(number & 1);
//return number % 2 == 0;
}
var oddCounter = 0;
var evenCounter = 0;
const SLOW_EVEN = "SLOW_EVEN";
console.time(SLOW_EVEN);
for (let index = 0; index < 100000000; index++) {
var res = isEven(index);
if (res) {
evenCounter++;
}
else {
oddCounter++;
}
}
console.timeEnd(SLOW_EVEN);
console.log(evenCounter);
console.log(oddCounter);
const FAST_EVEN = "FAST_EVEN";
evenCounter = 0;
oddCounter = 0;
console.time(FAST_EVEN);
for (let index = 0; index < 100000000; index++) {
var res = isEvenFast(index);
if (res) {
evenCounter++;
}
else {
oddCounter++;
}
}
console.timeEnd(FAST_EVEN);
console.log(evenCounter);
console.log(oddCounter);
console.log(isEven(148));