Skip to content

Commit 9662ead

Browse files
authored
Merge pull request #109 from haensl/108
#108: Add destructuring profile.
2 parents e933a59 + f96c34d commit 9662ead

File tree

8 files changed

+269
-14
lines changed

8 files changed

+269
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### 2.5.0
2+
* [#108: Add destructuring profile.](https://github.com/haensl/js-profiler/issues/108)
3+
14
### 2.4.2
25
* [#106: Add options to JSON result.](https://github.com/haensl/js-profiler/issues/106)
36
* Update dependencies.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ JS-Profiler powers [https://js-profiler.com](https://js-profiler.com).
2929
* [array concatenation](#array-concat)
3030
* [array copying](#array-copy)
3131
* [comparison operators](#comparison-operators)
32+
* [(de-)composition](#composition)
3233
* [guards](#guards)
3334
* [loops](#loops)
3435
* [map access](#map:access)
@@ -45,6 +46,8 @@ JS-Profiler powers [https://js-profiler.com](https://js-profiler.com).
4546

4647
## Updates<a name="updates"></a>
4748

49+
### [v2.5.0](https://github.com/haensl/js-profiler/releases/tag/v2.5.0): New profile: [(de-)composition.](#composition)<a name="new-in-v2.5.0"></a>
50+
4851
### [v2.3.0](https://github.com/haensl/js-profiler/releases/tag/v2.3.0): A new contributor and a new profile: [shallow array copying.](#array-copy)<a name="new-in-v2.3.0"></a>
4952

5053
We are happy to welcome [Josh Howe](https://github.com/joshtch) as a contributor to JS-Profiler! He added a [new profile comparing ways to shallow copy arrays](#array-copy).
@@ -246,6 +249,22 @@ Profiled operations:
246249
* `b = new Array(); for(...){ b.push(a[i]) }`
247250
* `b = new Array(a.length); for(...){ b[i] = a[i] }`
248251

252+
### [(de-)composition](https://js-profiler.com/#de-composition)<a name="composition"></a>
253+
(De-)Composition: composing objects, arrays and variables from each other.
254+
255+
Profiled operations:
256+
* `const { a, b } = obj`
257+
* `const { a = i } = obj`
258+
* `const [a, b] = arr`
259+
* `const [a = i, b] = d`
260+
* `const [a, b, ...tail] = d`
261+
* `const a = arr[i]`
262+
* `const a = arr[i] || j`
263+
* `const a = obj.b`
264+
* `const a = obj.b || i`
265+
* `const [a, b] = [b, a]`
266+
* `const c = b; b = a; a = c`
267+
249268
### [comarison operators](https://js-profiler.com/#comparison-operators)<a name="comparison-operators"></a>
250269
Variable comparison operators.
251270

docs/js-profiler.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
." vim: set syn=nroff
2-
.TH js-profiler 1 "February 2021" "js-profiler v2.4.2"
2+
.TH js-profiler 1 "June 2021" "js-profiler v2.5.0"
33

44
.SH NAME
55
js-profiler - A JavaScript profiling tool and collection of profiling modules and benchmarks.

lib/profiles/array-copy/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const copySlice = {
55
codeSample: 'a.slice()',
66
keywords: [
77
'array',
8+
'clone',
89
'copy',
910
'slice',
1011
'method'
@@ -16,9 +17,10 @@ const copySpread = {
1617
description: 'copy using Array spread syntax',
1718
keywords: [
1819
'array',
20+
'clone',
1921
'copy',
2022
'spread',
21-
'syntax',
23+
'syntax'
2224
].sort(),
2325
codeSample: '[...a]',
2426
f: (d) => [...d[0]]
@@ -28,6 +30,7 @@ const copyFrom = {
2830
description: 'copy using Array.from()',
2931
keywords: [
3032
'array',
33+
'clone',
3134
'copy',
3235
'from',
3336
'method'
@@ -40,6 +43,7 @@ const copyNewArray = {
4043
description: 'spread into Array constructor',
4144
keywords: [
4245
'array',
46+
'clone',
4347
'copy',
4448
'new',
4549
'constructor'
@@ -52,6 +56,7 @@ const copyConcatAB = {
5256
description: 'concatenate empty Array literal',
5357
keywords: [
5458
'array',
59+
'clone',
5560
'copy',
5661
'concat',
5762
'method'
@@ -64,6 +69,7 @@ const copyConcatBA = {
6469
description: 'concatenate onto empty Array literal',
6570
keywords: [
6671
'array',
72+
'clone',
6773
'copy',
6874
'concat',
6975
'method'
@@ -77,6 +83,7 @@ const copyPrependLiteral = {
7783
codeSample: 'b = []; Array.prototype.unshift.apply(b, a)',
7884
keywords: [
7985
'array',
86+
'clone',
8087
'copy',
8188
'literal',
8289
'apply',
@@ -95,6 +102,7 @@ const copyPrependPreallocate = {
95102
description: 'prepend to constructed Array',
96103
keywords: [
97104
'array',
105+
'clone',
98106
'copy',
99107
'preallocate',
100108
'apply',
@@ -116,6 +124,7 @@ const copyAppendLiteral = {
116124
description: 'append to Array literal using spread',
117125
keywords: [
118126
'array',
127+
'clone',
119128
'copy',
120129
'literal',
121130
'apply',
@@ -136,6 +145,7 @@ const copyAppendSpreadPreallocate = {
136145
description: 'append to constructed Array using spread',
137146
keywords: [
138147
'array',
148+
'clone',
139149
'copy',
140150
'preallocate',
141151
'constructor',
@@ -157,6 +167,7 @@ const copyAppendForPreallocate = {
157167
description: 'append to constructed Array in a for loop',
158168
keywords: [
159169
'array',
170+
'clone',
160171
'copy',
161172
'preallocate',
162173
'constructor',
@@ -182,6 +193,7 @@ const copyAppendForLiteral = {
182193
description: 'append to Array literal in a for loop',
183194
keywords: [
184195
'array',
196+
'clone',
185197
'copy',
186198
'push',
187199
'for',
@@ -207,6 +219,7 @@ const copySetForPreallocate = {
207219
description: 'preallocate new Array and assign values in a for loop',
208220
keywords: [
209221
'array',
222+
'clone',
210223
'copy',
211224
'preallocate',
212225
'set',
@@ -248,8 +261,8 @@ const functions = [
248261
module.exports = {
249262
name: 'array copying',
250263
description: {
251-
long: 'Array copying variations: creating a new array with the same elements as an existing array.',
252-
short: 'Array copying variations.',
264+
long: 'Array copying/cloning variations: creating a new array with the same elements as an existing array.',
265+
short: 'Array copying/cloning variations.'
253266
},
254267
keywords: unique(
255268
functions.map((fn) => fn.keywords)

lib/profiles/composition/index.js

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/* eslint-disable no-unused-vars */
2+
const { unique } = require('../../support/array');
3+
4+
const destructureObject = {
5+
description: 'Destructuring an Object',
6+
codeSample: 'const { a, b } = obj',
7+
keywords: [
8+
'assignment',
9+
'object',
10+
'destructuring',
11+
'decomposition',
12+
'composition'
13+
].sort(),
14+
f: (d) => {
15+
const { num, obj } = d;
16+
},
17+
testDataType: 'object'
18+
};
19+
20+
const destructureObjectDefault = {
21+
description: 'Destructuring an Object with default values',
22+
codeSample: 'const { a = i } = obj',
23+
keywords: [
24+
'assignment',
25+
'object',
26+
'destructuring',
27+
'decomposition',
28+
'composition',
29+
'default',
30+
'values'
31+
].sort(),
32+
f: (d) => {
33+
const { num = 5, foo = 'bar' } = d;
34+
},
35+
testDataType: 'object'
36+
};
37+
38+
const destructureArray = {
39+
description: 'Destructuring an Array',
40+
codeSample: 'const [a,b] = arr',
41+
keywords: [
42+
'assignment',
43+
'array',
44+
'destructuring',
45+
'decomposition',
46+
'composition'
47+
].sort(),
48+
f: (d) => {
49+
const [a, b] = d;
50+
},
51+
testDataType: 'array'
52+
};
53+
54+
const destructureArrayDefault = {
55+
description: 'Destructuring an Array with default values',
56+
codeSample: 'const [a = i, b] = arr',
57+
keywords: [
58+
'assignment',
59+
'array',
60+
'destructuring',
61+
'decomposition',
62+
'composition',
63+
'default',
64+
'values'
65+
].sort(),
66+
f: (d) => {
67+
const [a = 5, b] = d;
68+
},
69+
testDataType: 'array'
70+
};
71+
72+
const destructureArrayTail = {
73+
description: 'Destructuring an Array with tail',
74+
codeSample: 'const [a,b, ...tail] = arr',
75+
keywords: [
76+
'assignment',
77+
'array',
78+
'destructuring',
79+
'decomposition',
80+
'composition',
81+
'rest',
82+
'tail'
83+
].sort(),
84+
f: (d) => {
85+
const [a, b, ...tail] = d;
86+
},
87+
testDataType: 'array'
88+
};
89+
90+
const assignArray = {
91+
description: 'Assignment from array items',
92+
codeSample: 'const a = arr[i]',
93+
keywords: [
94+
'array',
95+
'assignment',
96+
'decomposition',
97+
'composition'
98+
].sort(),
99+
f: (d) => {
100+
const a = d[0];
101+
const b = d[1];
102+
},
103+
testDataType: 'array'
104+
};
105+
106+
const assignArrayDefault = {
107+
description: 'Assignment from array items with default',
108+
codeSample: 'const a = arr[i] || j',
109+
keywords: [
110+
'array',
111+
'assignment',
112+
'decomposition',
113+
'composition',
114+
'default',
115+
'values'
116+
].sort(),
117+
f: (d) => {
118+
const a = d[0] || 5;
119+
const b = d[1];
120+
},
121+
testDataType: 'array'
122+
};
123+
124+
const assignObject = {
125+
description: 'Assignment from object properties',
126+
codeSample: 'const a = obj.b',
127+
keywords: [
128+
'object',
129+
'assignment',
130+
'decomposition',
131+
'composition'
132+
].sort(),
133+
f: (d) => {
134+
const str = d.obj.str;
135+
},
136+
testDataType: 'object'
137+
};
138+
139+
const assignObjectDefault = {
140+
description: 'Assignment from object properties with default',
141+
codeSample: 'const a = obj.b || i',
142+
keywords: [
143+
'object',
144+
'assignment',
145+
'decomposition',
146+
'composition',
147+
'default',
148+
'values'
149+
].sort(),
150+
f: (d) => {
151+
const str = d.obj.foo || 'bar';
152+
},
153+
testDataType: 'object'
154+
};
155+
156+
const destructureSwapArray = {
157+
description: 'Swapping variables via Array destructuring',
158+
codeSample: 'const [a, b] = [b, a]',
159+
keywords: [
160+
'array',
161+
'destructuring',
162+
'swap',
163+
'variables',
164+
'composition',
165+
'decomposition'
166+
],
167+
f: (d) => {
168+
let a = d[0];
169+
let b = d[1];
170+
[a, b] = [b, a];
171+
},
172+
testDataType: 'array'
173+
};
174+
175+
const assignSwapArray = {
176+
description: 'Swapping variables via assignment',
177+
codeSample: 'const c = b; b = a; a = c;',
178+
keywords: [
179+
'swap',
180+
'variables',
181+
'composition',
182+
'decomposition'
183+
],
184+
f: (d) => {
185+
let a = d[0];
186+
let b = d[1];
187+
const c = b;
188+
b = a;
189+
a = c;
190+
},
191+
testDataType: 'array'
192+
};
193+
194+
const functions = [
195+
destructureArray,
196+
destructureArrayDefault,
197+
destructureArrayTail,
198+
destructureObject,
199+
destructureObjectDefault,
200+
destructureSwapArray,
201+
assignArray,
202+
assignArrayDefault,
203+
assignObject,
204+
assignObjectDefault,
205+
assignSwapArray
206+
];
207+
208+
module.exports = {
209+
name: '(de-)composition',
210+
description: {
211+
short: '(De-)composing objects, variables and arrays.',
212+
long: '(De-)composing objects, variables and arrays from each other.'
213+
},
214+
keywords: unique(
215+
functions.map((fn) => fn.keywords)
216+
.reduce((keywords, fnKeywords) => [...keywords, ...fnKeywords])
217+
).sort(),
218+
functions
219+
};
220+
/* eslint-enable no-unused-vars */

0 commit comments

Comments
 (0)