data-structure-typed 1.41.3 → 1.41.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +561 -265
  3. package/benchmark/report.html +79 -0
  4. package/benchmark/report.json +343 -28
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.js +11 -10
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  7. package/dist/mjs/data-structures/binary-tree/binary-tree.js +11 -10
  8. package/dist/umd/data-structure-typed.min.js +1 -1
  9. package/dist/umd/data-structure-typed.min.js.map +1 -1
  10. package/package.json +8 -4
  11. package/src/data-structures/binary-tree/binary-tree.ts +12 -9
  12. package/test/config.ts +1 -1
  13. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +36 -0
  14. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +45 -0
  15. package/test/performance/data-structures/binary-tree/bst.test.ts +36 -0
  16. package/test/performance/data-structures/graph/directed-graph.test.ts +49 -0
  17. package/test/performance/data-structures/heap/heap.test.ts +30 -0
  18. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +40 -0
  19. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +34 -0
  20. package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +19 -0
  21. package/test/performance/data-structures/queue/deque.test.ts +8 -6
  22. package/test/performance/data-structures/queue/queue.test.ts +17 -12
  23. package/test/performance/data-structures/trie/trie.test.ts +22 -0
  24. package/test/performance/reportor.ts +183 -0
  25. package/test/performance/types/index.ts +1 -0
  26. package/test/performance/types/reportor.ts +3 -0
  27. package/test/types/utils/index.ts +1 -0
  28. package/test/types/utils/json2html.ts +1 -0
  29. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +1 -1
  30. package/test/unit/data-structures/heap/heap.test.ts +5 -4
  31. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -23
  32. package/test/unit/data-structures/linked-list/linked-list.test.ts +3 -30
  33. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -21
  34. package/test/unit/data-structures/matrix/matrix2d.test.ts +1 -1
  35. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +0 -32
  36. package/test/unit/data-structures/queue/queue.test.ts +3 -39
  37. package/test/utils/array.ts +5514 -0
  38. package/test/utils/big-o.ts +14 -8
  39. package/test/utils/console.ts +31 -0
  40. package/test/utils/index.ts +5 -0
  41. package/test/utils/is.ts +56 -0
  42. package/test/utils/json2html.ts +322 -0
  43. package/test/utils/number.ts +10 -0
  44. package/test/utils/string.ts +1 -0
  45. package/test/config.js +0 -4
  46. package/test/performance/index.ts +0 -47
  47. package/test/types/index.js +0 -29
  48. package/test/types/utils/big-o.js +0 -2
  49. package/test/types/utils/index.js +0 -29
  50. package/test/utils/big-o.js +0 -222
  51. package/test/utils/index.js +0 -30
  52. package/test/utils/number.js +0 -14
@@ -1,222 +0,0 @@
1
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', {value: true});
3
- exports.logBigOMetrics = exports.logBigOMetricsWrap = exports.bigO = exports.magnitude = void 0;
4
- var config_1 = require('../config');
5
-
6
- var isDebug = config_1.isDebugTest;
7
- var orderReducedBy = 2; // reduction of bigO's order compared to the baseline bigO
8
- exports.magnitude = {
9
- CONSTANT: Math.floor(Number.MAX_SAFE_INTEGER / Math.pow(10, orderReducedBy)),
10
- LOG_N: Math.pow(10, 9 - orderReducedBy),
11
- LINEAR: Math.pow(10, 6 - orderReducedBy),
12
- N_LOG_N: Math.pow(10, 5 - orderReducedBy),
13
- SQUARED: Math.pow(10, 4 - orderReducedBy),
14
- CUBED: Math.pow(10, 3 - orderReducedBy),
15
- FACTORIAL: 20 - orderReducedBy
16
- };
17
- exports.bigO = {
18
- CONSTANT: exports.magnitude.CONSTANT / 100000,
19
- LOG_N: Math.log2(exports.magnitude.LOG_N) / 1000,
20
- LINEAR: exports.magnitude.LINEAR / 1000,
21
- N_LOG_N: (exports.magnitude.N_LOG_N * Math.log2(exports.magnitude.LOG_N)) / 1000,
22
- SQUARED: Math.pow(exports.magnitude.SQUARED, 2) / 1000,
23
- CUBED: Math.pow(exports.magnitude.SQUARED, 3) / 1000,
24
- FACTORIAL: 10000
25
- };
26
-
27
- function findPotentialN(input) {
28
- var longestArray = [];
29
- var mostProperties = {};
30
-
31
- function recurse(obj) {
32
- if (Array.isArray(obj)) {
33
- if (obj.length > longestArray.length) {
34
- longestArray = obj;
35
- }
36
- } else if (typeof obj === 'object' && obj !== null) {
37
- var keys = Object.keys(obj);
38
- if (keys.length > Object.keys(mostProperties).length) {
39
- mostProperties = obj;
40
- }
41
- keys.forEach(function (key) {
42
- recurse(obj[key]);
43
- });
44
- }
45
- }
46
-
47
- if (Array.isArray(input)) {
48
- input.forEach(function (item) {
49
- recurse(item);
50
- });
51
- } else {
52
- recurse(input);
53
- }
54
- // return [longestArray, mostProperties] : [any[], { [key: string]: any }];
55
- return Math.max(longestArray.length, Object.keys(mostProperties).length);
56
- }
57
-
58
- function linearRegression(x, y) {
59
- var n = x.length;
60
- var sumX = x.reduce(function (acc, val) {
61
- return acc + val;
62
- }, 0);
63
- var sumY = y.reduce(function (acc, val) {
64
- return acc + val;
65
- }, 0);
66
- var sumXSquared = x.reduce(function (acc, val) {
67
- return acc + Math.pow(val, 2);
68
- }, 0);
69
- var sumXY = x.reduce(function (acc, val, i) {
70
- return acc + val * y[i];
71
- }, 0);
72
- var slope = (n * sumXY - sumX * sumY) / (n * sumXSquared - Math.pow(sumX, 2));
73
- var intercept = (sumY - slope * sumX) / n;
74
- var yHat = x.map(function (val) {
75
- return slope * val + intercept;
76
- });
77
- var totalVariation = y
78
- .map(function (val, i) {
79
- return Math.pow(val - yHat[i], 2);
80
- })
81
- .reduce(function (acc, val) {
82
- return acc + val;
83
- }, 0);
84
- var explainedVariation = y
85
- .map(function (val) {
86
- return Math.pow(val - sumY / n, 2);
87
- })
88
- .reduce(function (acc, val) {
89
- return acc + val;
90
- }, 0);
91
- var rSquared = 1 - totalVariation / explainedVariation;
92
- return {slope: slope, intercept: intercept, rSquared: rSquared};
93
- }
94
-
95
- function estimateBigO(runtimes, dataSizes) {
96
- // Make sure the input runtimes and data sizes have the same length
97
- if (runtimes.length !== dataSizes.length) {
98
- return 'Lengths of input arrays do not match';
99
- }
100
- // Create an array to store the computational complexity of each data point
101
- var complexities = [];
102
- // Traverse different possible complexities
103
- var complexitiesToCheck = [
104
- 'O(1)',
105
- 'O(log n)',
106
- 'O(n)',
107
- 'O(n log n)',
108
- 'O(n^2)' // squared time complexity
109
- ];
110
- var _loop_1 = function (complexity) {
111
- // Calculate data points for fitting
112
- var fittedData = dataSizes.map(function (size) {
113
- if (complexity === 'O(1)') {
114
- return 1; // constant time complexity
115
- } else if (complexity === 'O(log n)') {
116
- return Math.log(size);
117
- } else if (complexity === 'O(n)') {
118
- return size;
119
- } else if (complexity === 'O(n log n)') {
120
- return size * Math.log(size);
121
- } else if (complexity === 'O(n^2)') {
122
- return Math.pow(size, 2);
123
- } else {
124
- return Math.pow(size, 10);
125
- }
126
- });
127
- // Fit the data points using linear regression analysis
128
- var regressionResult = linearRegression(fittedData, runtimes);
129
- // Check the R-squared value of the fit. It is usually considered a valid fit if it is greater than 0.9.
130
- if (regressionResult.rSquared >= 0.9) {
131
- complexities.push(complexity);
132
- }
133
- };
134
- for (var _i = 0, complexitiesToCheck_1 = complexitiesToCheck; _i < complexitiesToCheck_1.length; _i++) {
135
- var complexity = complexitiesToCheck_1[_i];
136
- _loop_1(complexity);
137
- }
138
- // If there is no valid fitting result, return "cannot estimate", otherwise return the estimated time complexity
139
- if (complexities.length === 0) {
140
- return 'Unable to estimate';
141
- } else {
142
- return complexities.join(' or ');
143
- }
144
- }
145
-
146
- var methodLogs = new Map();
147
-
148
- function logBigOMetricsWrap(fn, args, fnName) {
149
- var startTime = performance.now();
150
- var result = fn(args);
151
- var endTime = performance.now();
152
- var runTime = endTime - startTime;
153
- var methodName = ''.concat(fnName);
154
- if (!methodLogs.has(methodName)) {
155
- methodLogs.set(methodName, []);
156
- }
157
- var methodLog = methodLogs.get(methodName);
158
- var maxDataSize = args.length === 1 && typeof args[0] === 'number' ? args[0] : findPotentialN(args);
159
- if (methodLog) {
160
- methodLog.push([runTime, maxDataSize]);
161
- if (methodLog.length >= 20) {
162
- isDebug && console.log('triggered', methodName, methodLog);
163
- var bigO_1 = estimateBigO(
164
- methodLog.map(function (_a) {
165
- var runTime = _a[0];
166
- return runTime;
167
- }),
168
- methodLog.map(function (_a) {
169
- var runTime = _a[0];
170
- return runTime;
171
- })
172
- );
173
- isDebug && console.log('Estimated Big O: '.concat(bigO_1));
174
- methodLogs.delete(methodName);
175
- }
176
- }
177
- return result;
178
- }
179
-
180
- exports.logBigOMetricsWrap = logBigOMetricsWrap;
181
-
182
- function logBigOMetrics(target, propertyKey, descriptor) {
183
- var originalMethod = descriptor.value;
184
- descriptor.value = function () {
185
- var args = [];
186
- for (var _i = 0; _i < arguments.length; _i++) {
187
- args[_i] = arguments[_i];
188
- }
189
- var startTime = performance.now();
190
- var result = originalMethod.apply(this, args);
191
- var endTime = performance.now();
192
- var runTime = endTime - startTime;
193
- var methodName = ''.concat(target.constructor.name, '.').concat(propertyKey);
194
- if (!methodLogs.has(methodName)) {
195
- methodLogs.set(methodName, []);
196
- }
197
- var methodLog = methodLogs.get(methodName);
198
- var maxDataSize = args.length === 1 && typeof args[0] === 'number' ? args[0] : findPotentialN(args);
199
- if (methodLog) {
200
- methodLog.push([runTime, maxDataSize]);
201
- if (methodLog.length >= 20) {
202
- isDebug && console.log('triggered', methodName, methodLog);
203
- var bigO_2 = estimateBigO(
204
- methodLog.map(function (_a) {
205
- var runTime = _a[0];
206
- return runTime;
207
- }),
208
- methodLog.map(function (_a) {
209
- var runTime = _a[0];
210
- return runTime;
211
- })
212
- );
213
- isDebug && console.log('Estimated Big O: '.concat(bigO_2));
214
- methodLogs.delete(methodName);
215
- }
216
- }
217
- return result;
218
- };
219
- return descriptor;
220
- }
221
-
222
- exports.logBigOMetrics = logBigOMetrics;
@@ -1,30 +0,0 @@
1
- 'use strict';
2
- var __createBinding =
3
- (this && this.__createBinding) ||
4
- (Object.create
5
- ? function (o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- var desc = Object.getOwnPropertyDescriptor(m, k);
8
- if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
- desc = {
10
- enumerable: true,
11
- get: function () {
12
- return m[k];
13
- }
14
- };
15
- }
16
- Object.defineProperty(o, k2, desc);
17
- }
18
- : function (o, m, k, k2) {
19
- if (k2 === undefined) k2 = k;
20
- o[k2] = m[k];
21
- });
22
- var __exportStar =
23
- (this && this.__exportStar) ||
24
- function (m, exports) {
25
- for (var p in m)
26
- if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
- };
28
- Object.defineProperty(exports, '__esModule', {value: true});
29
- __exportStar(require('./number'), exports);
30
- __exportStar(require('./big-o'), exports);
@@ -1,14 +0,0 @@
1
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', {value: true});
3
- exports.getMSB = exports.getRandomInt = void 0;
4
- function getRandomInt(min, max) {
5
- return Math.floor(Math.random() * (max - min + 1)) + min;
6
- }
7
- exports.getRandomInt = getRandomInt;
8
- var getMSB = function (value) {
9
- if (value <= 0) {
10
- return 0;
11
- }
12
- return 1 << (31 - Math.clz32(value));
13
- };
14
- exports.getMSB = getMSB;