@vicin/sigil 3.4.0 → 4.0.0
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.
- package/CHANGELOG.md +4 -252
- package/CONTRIBUTING.md +24 -0
- package/LICENSE +3 -1
- package/README.md +134 -180
- package/dist/index.cjs +269 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{index.d.mts → index.d.cts} +67 -242
- package/dist/index.d.ts +67 -242
- package/dist/index.global.js +88 -208
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +87 -219
- package/dist/index.js.map +1 -1
- package/package.json +43 -59
- package/dist/index.mjs +0 -374
- package/dist/index.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
# Sigil
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/@vicin/sigil) [](https://www.npmjs.com/package/@vicin/sigil) [](LICENSE)  [](https://www.npmjs.com/package/@vicin/sigil) [](https://www.npmjs.com/package/@vicin/sigil) [](LICENSE)  [](https://github.com/ZiadTaha62/vicin-packages/actions/workflows/ci.yml) 
|
|
4
4
|
|
|
5
|
-
> - 🎉
|
|
5
|
+
> - 🎉 v4.0.0 is out! Happy coding! 😄💻
|
|
6
6
|
> - 📄 **Changelog:** [CHANGELOG.md](./CHANGELOG.md)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
**Sigil** — bulletproof class identity for large TypeScript projects.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Reliable `instanceof`-style checks that survive bundling, HMR, monorepos and realms + exact/subtype discrimination + lightweight nominal typing + and debug/log friendly labels/lineage.
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
12
|
+
## Why Sigil?
|
|
13
|
+
|
|
14
|
+
- **Works when `instanceof` breaks** (multi-bundle, HMR, separate realms)
|
|
15
|
+
- **Exact class match (`isExactInstance`)** — not just "inherits from"
|
|
16
|
+
- **One-line nominal branding** (`declare [sigil]: ExtendSigil<…>`)
|
|
17
|
+
- **Human-readable class IDs in logs & debugging** (`SigilLabel`/`SigilLineage`)
|
|
18
|
+
- **Tiny (~0.9 kB brotli)**
|
|
19
|
+
- **Fast (near native `instanceof` speed)**
|
|
20
|
+
- **100% test coverage**
|
|
21
|
+
|
|
22
|
+
## Limitations
|
|
23
|
+
|
|
24
|
+
- **Identity is explicit and dependent on passed labels**, If same label is passed to two different classes `Sigil` will treat them as one
|
|
25
|
+
- **Identity is updated with every new label passed only**, If you stopped passing labels to child classes their identity will stop at last passed label
|
|
26
|
+
- **`Sigil` is not built for security**, identity can be forged
|
|
27
|
+
|
|
28
|
+
Earlier versions of `Sigil` tried to solve these limitations, however it was not `100%` reliable, so we decided to make this package minimal and stable, while adding new features in future package `@vicin/sigil-extend`
|
|
18
29
|
|
|
19
30
|
---
|
|
20
31
|
|
|
@@ -115,12 +126,8 @@ class User extends Sigil {}
|
|
|
115
126
|
attachSigil(User, '@myorg/mypkg.User');
|
|
116
127
|
```
|
|
117
128
|
|
|
118
|
-
> Note: Function pattern is susceptible to some [Edge case subtle pitfalls](#edge-cases) if not used appropriately, so we advise to use decorator pattern
|
|
119
|
-
|
|
120
129
|
### Migration
|
|
121
130
|
|
|
122
|
-
Migrating old code into `Sigil` can be done with extra couple lines of code only:
|
|
123
|
-
|
|
124
131
|
1. Pass your base class to `Sigilify` mixin:
|
|
125
132
|
|
|
126
133
|
```ts
|
|
@@ -137,7 +144,7 @@ import { Sigil } from '@vicin/sigil';
|
|
|
137
144
|
class MyBaseClass extends Sigil {} // <-- add 'extends Sigil' here
|
|
138
145
|
```
|
|
139
146
|
|
|
140
|
-
Congratulations — you’ve opted into `Sigil
|
|
147
|
+
Congratulations — you’ve opted into `Sigil`, now you can start giving your classes identity by using `attachSigil` or `AttachSigil` helpers.
|
|
141
148
|
|
|
142
149
|
---
|
|
143
150
|
|
|
@@ -146,16 +153,15 @@ Congratulations — you’ve opted into `Sigil` and you can start replacing `ins
|
|
|
146
153
|
### Terminology
|
|
147
154
|
|
|
148
155
|
- **Label**: An identity (string) such as `@scope/pkg.ClassName`, must be unique for each `Sigil` class otherwise error is thrown.
|
|
149
|
-
- **
|
|
150
|
-
- **
|
|
151
|
-
- **isExactType**: Takes object argument and check if this object is an instance of calling class only. Can be called from class instances as well.
|
|
156
|
+
- **isInstance**: Takes object argument and check if this object is an instance of calling class or it's children. Can be called from class instances as well.
|
|
157
|
+
- **isExactInstance**: Takes object argument and check if this object is an instance of calling class only. Can be called from class instances as well.
|
|
152
158
|
- **[sigil]**: TypeScript symbol marker for nominal types.
|
|
153
159
|
|
|
154
160
|
---
|
|
155
161
|
|
|
156
162
|
### Purpose and Origins
|
|
157
163
|
|
|
158
|
-
Sigil addresses issues in large monorepos
|
|
164
|
+
Sigil addresses issues in large codebases and monorepos:
|
|
159
165
|
|
|
160
166
|
- **Unreliable `instanceof`:** Bundling cause class redefinitions, breaking checks.
|
|
161
167
|
|
|
@@ -164,8 +170,8 @@ Sigil addresses issues in large monorepos, HMR:
|
|
|
164
170
|
if (obj instanceof User) { ... }
|
|
165
171
|
|
|
166
172
|
// With Sigil
|
|
167
|
-
if (User.
|
|
168
|
-
if (User.
|
|
173
|
+
if (User.isInstance(obj)) { ... } // This still works even if User was bundled twice.
|
|
174
|
+
if (User.isExactInstance(obj)) { ... } // Or check for exactly same constructor not its children
|
|
169
175
|
```
|
|
170
176
|
|
|
171
177
|
Also by utilizing unique passed labels it solves another problem in Domain-Driven Design (DDD):
|
|
@@ -183,6 +189,8 @@ type test1 = User extends Sigil ? true : false; // true
|
|
|
183
189
|
type test2 = Sigil extends User ? true : false; // false
|
|
184
190
|
```
|
|
185
191
|
|
|
192
|
+
- **Need for stable class id**: Most large projects implement their own labels (e.g. to be used in logs)
|
|
193
|
+
|
|
186
194
|
### Implementation Mechanics
|
|
187
195
|
|
|
188
196
|
- **Runtime Contract:** Established via extending `Sigil` or using `Sigilify` mixin.
|
|
@@ -228,22 +236,22 @@ const admin = new Admin();
|
|
|
228
236
|
const user = new User();
|
|
229
237
|
|
|
230
238
|
// Instanceof like behavior
|
|
231
|
-
console.log(Admin.
|
|
232
|
-
console.log(Admin.
|
|
233
|
-
console.log(User.
|
|
234
|
-
console.log(User.
|
|
239
|
+
console.log(Admin.isInstance(admin)); // true
|
|
240
|
+
console.log(Admin.isInstance(user)); // false
|
|
241
|
+
console.log(User.isInstance(admin)); // true
|
|
242
|
+
console.log(User.isInstance(user)); // true
|
|
235
243
|
|
|
236
244
|
// Exact checks
|
|
237
|
-
console.log(Admin.
|
|
238
|
-
console.log(Admin.
|
|
239
|
-
console.log(User.
|
|
240
|
-
console.log(User.
|
|
245
|
+
console.log(Admin.isExactInstance(admin)); // true
|
|
246
|
+
console.log(Admin.isExactInstance(user)); // false
|
|
247
|
+
console.log(User.isExactInstance(user)); // true
|
|
248
|
+
console.log(User.isExactInstance(admin)); // false (Admin is child indeed but this checks for user specifically)
|
|
241
249
|
|
|
242
250
|
// Can use checks from instances
|
|
243
|
-
console.log(admin.
|
|
244
|
-
console.log(user.
|
|
245
|
-
console.log(admin.
|
|
246
|
-
console.log(user.
|
|
251
|
+
console.log(admin.isInstance(user)); // false
|
|
252
|
+
console.log(user.isInstance(admin)); // true
|
|
253
|
+
console.log(admin.isExactInstance(user)); // false
|
|
254
|
+
console.log(user.isExactInstance(admin)); // false
|
|
247
255
|
|
|
248
256
|
// Type checks are nominal
|
|
249
257
|
type test1 = Admin extends User ? true : false; // true
|
|
@@ -252,11 +260,9 @@ type test2 = User extends Admin ? true : false; // false
|
|
|
252
260
|
// Passed label must be unique (enforced by Sigil) so can be used as stable Id for class
|
|
253
261
|
// Also 'SigilLabelLineage' is useful for logging & debugging
|
|
254
262
|
console.log(Admin.SigilLabel); // '@myorg/Admin'
|
|
255
|
-
console.log(Admin.SigilEffectiveLabel); // '@myorg/Admin'
|
|
256
263
|
console.log(Admin.SigilLabelLineage); // ['Sigil', '@myorg/User', '@myorg/Admin']
|
|
257
|
-
console.log(admin.
|
|
258
|
-
console.log(admin.
|
|
259
|
-
console.log(admin.getSigilLabelLineage()); // ['Sigil', '@myorg/User', '@myorg/Admin']
|
|
264
|
+
console.log(admin.SigilLabel); // '@myorg/Admin'
|
|
265
|
+
console.log(admin.SigilLabelLineage); // ['Sigil', '@myorg/User', '@myorg/Admin']
|
|
260
266
|
```
|
|
261
267
|
|
|
262
268
|
### Errors & throws
|
|
@@ -286,25 +292,6 @@ class A {}
|
|
|
286
292
|
attachSigil(class A {}); // Throws: [Sigil Error] 'AttachSigil' function accept only Sigil classes but used on class 'A'
|
|
287
293
|
```
|
|
288
294
|
|
|
289
|
-
#### No label is passed with `autofillLabels: false`
|
|
290
|
-
|
|
291
|
-
```ts
|
|
292
|
-
updateSigilOptions({ autofillLabels: false });
|
|
293
|
-
|
|
294
|
-
class A extends Sigil {}
|
|
295
|
-
new A(); // Throws: [Sigil Error] Class 'A' is not sigilified, Make sure to sigilify all Sigil classes or set 'autofillLabels' to 'true'
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
#### Same label is passed twice to `Sigil`
|
|
299
|
-
|
|
300
|
-
```ts
|
|
301
|
-
@AttachSigil('Label')
|
|
302
|
-
class A extends Sigil {}
|
|
303
|
-
|
|
304
|
-
@AttachSigil('Label')
|
|
305
|
-
class B extends Sigil {} // Throws: [Sigil Error] Passed label 'Label' to class 'B' is re-used, passed labels must be unique
|
|
306
|
-
```
|
|
307
|
-
|
|
308
295
|
#### Invalid label format
|
|
309
296
|
|
|
310
297
|
```ts
|
|
@@ -314,19 +301,14 @@ updateSigilOptions({ labelValidation: RECOMMENDED_LABEL_REGEX });
|
|
|
314
301
|
class A extends Sigil {} // Throws: [Sigil Error] Invalid Sigil label 'InvalidLabel'. Make sure that supplied label matches validation regex or function
|
|
315
302
|
```
|
|
316
303
|
|
|
317
|
-
####
|
|
304
|
+
#### Attach sigil to parent after child
|
|
318
305
|
|
|
319
306
|
```ts
|
|
320
|
-
|
|
321
|
-
class
|
|
322
|
-
```
|
|
307
|
+
class Parent extends Sigil {}
|
|
308
|
+
class Child extends Parent {}
|
|
323
309
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
```ts
|
|
327
|
-
updateSigilOptions({ autofillLabels: {} as any }); // Throws: 'updateSigilOptions.autofillLabels' must be boolean
|
|
328
|
-
updateSigilOptions({ labelValidation: 123 as any }); // Throws: 'updateSigilOptions.labelValidation' must be null, function or RegExp
|
|
329
|
-
updateSigilOptions({ skipLabelUniquenessCheck: 'str' as any }); // Throws: 'updateSigilOptions.skipLabelUniquenessCheck' must be boolean
|
|
310
|
+
attachSigil(Child, 'Child');
|
|
311
|
+
attachSigil(Parent, 'Parent'); // Throws: [Sigil Error] Class 'Parent' with label 'Sigil' is already sigilified
|
|
330
312
|
```
|
|
331
313
|
|
|
332
314
|
---
|
|
@@ -352,16 +334,13 @@ updateSigilOptions({ skipLabelUniquenessCheck: 'str' as any }); // Throws: 'upda
|
|
|
352
334
|
- **Helpers:**
|
|
353
335
|
- `isSigilCtor(ctor)`
|
|
354
336
|
- `isSigilInstance(inst)`
|
|
355
|
-
- `
|
|
337
|
+
- `hasOwnSigil(ctor)`
|
|
356
338
|
|
|
357
339
|
- **Options:**
|
|
358
340
|
- `updateSigilOptions(opts)`
|
|
359
341
|
- `RECOMMENDED_LABEL_REGEX`
|
|
360
342
|
|
|
361
343
|
- **Types:**
|
|
362
|
-
- `ISigil<Label, ParentSigil?>`
|
|
363
|
-
- `ISigilStatic<Label, ParentSigil?>`
|
|
364
|
-
- `ISigilInstance<Label, ParentSigil?>`
|
|
365
344
|
- `SigilOf<T>`
|
|
366
345
|
- `ExtendSigil<Label, Parent>`
|
|
367
346
|
- `GetPrototype<Class>`
|
|
@@ -377,8 +356,8 @@ updateSigilOptions({ skipLabelUniquenessCheck: 'str' as any }); // Throws: 'upda
|
|
|
377
356
|
- `attachSigil(Class, label, opts?)`: function that validates and decorates an existing class constructor.
|
|
378
357
|
- `isSigilCtor(value)`: `true` if `value` is a `Sigil` constructor.
|
|
379
358
|
- `isSigilInstance(value)`: `true` if `value` is an instance of a `Sigil` constructor.
|
|
380
|
-
- `
|
|
381
|
-
- `updateSigilOptions(opts)`: change global runtime options of `Sigil` library (e.g., `
|
|
359
|
+
- `hasOwnSigil(ctor)`: `true` if new sigil label is attached to `ctor`
|
|
360
|
+
- `updateSigilOptions(opts)`: change global runtime options of `Sigil` library (e.g., `labelValidation`).
|
|
382
361
|
- `RECOMMENDED_LABEL_REGEX`: regex that ensures structure of `@scope/package.ClassName` to all labels, it's advised to use it as your `SigilOptions.labelValidation`
|
|
383
362
|
|
|
384
363
|
### Instance & static helpers provided by Sigilified constructors
|
|
@@ -386,18 +365,18 @@ updateSigilOptions({ skipLabelUniquenessCheck: 'str' as any }); // Throws: 'upda
|
|
|
386
365
|
When a constructor is sigilified it will expose the following **static** getters/methods:
|
|
387
366
|
|
|
388
367
|
- `SigilLabel` — the identity label string.
|
|
389
|
-
- `SigilEffectiveLabel` — the human label string.
|
|
390
368
|
- `SigilLabelLineage` — readonly array of labels representing parent → child for debugging.
|
|
391
|
-
- `
|
|
392
|
-
- `
|
|
369
|
+
- `hasOwnSigil` — check if new sigil label is attached to this class.
|
|
370
|
+
- `isInstance(other)` — check if other is an instance of this constructor or its children.
|
|
371
|
+
- `isExactInstance(other) `— check if other is an instance exactly this constructor.
|
|
393
372
|
|
|
394
373
|
Instances of sigilified classes expose instance helpers:
|
|
395
374
|
|
|
396
|
-
- `
|
|
397
|
-
- `
|
|
398
|
-
- `
|
|
399
|
-
- `
|
|
400
|
-
- `
|
|
375
|
+
- `SigilLabel` — the identity label string.
|
|
376
|
+
- `SigilLabelLineage` — readonly array of labels representing parent → child for debugging.
|
|
377
|
+
- `hasOwnSigil` — check if new sigil label is attached to this class.
|
|
378
|
+
- `isInstance(other)` — check if other is an instance of the same class or its children as this.
|
|
379
|
+
- `isExactInstance(other) `— check if other is an instance exactly the same constructor.
|
|
401
380
|
|
|
402
381
|
---
|
|
403
382
|
|
|
@@ -409,9 +388,7 @@ Customize behavior globally at startup:
|
|
|
409
388
|
import { updateSigilOptions } from '@vicin/sigil';
|
|
410
389
|
|
|
411
390
|
updateSigilOptions({
|
|
412
|
-
autofillLabels: true, // Automatically label unlabeled subclasses
|
|
413
391
|
labelValidation: null, // Function or regex, Enforce label format
|
|
414
|
-
skipLabelUniquenessCheck: false, // Skip uniqueness check for labels, should be used in HMR set-ups only
|
|
415
392
|
});
|
|
416
393
|
```
|
|
417
394
|
|
|
@@ -419,66 +396,28 @@ Values defined in previous example are defaults, per-class overrides available i
|
|
|
419
396
|
|
|
420
397
|
---
|
|
421
398
|
|
|
422
|
-
##
|
|
423
|
-
|
|
424
|
-
By default `Sigil` works with minimal mode, You can ignore all decorators and functions and just make base class extend `Sigil`:
|
|
425
|
-
|
|
426
|
-
```ts
|
|
427
|
-
import { Sigil, updateSigilOptions } from '@vicin/sigil';
|
|
428
|
-
|
|
429
|
-
// No decorators or functions needed to use 'isOfType' ('instanceof' replacement)
|
|
430
|
-
class A extends Sigil {}
|
|
431
|
-
class B extends A {}
|
|
432
|
-
class C extends B {}
|
|
433
|
-
```
|
|
434
|
-
|
|
435
|
-
## Strict mode
|
|
436
|
-
|
|
437
|
-
If you want to enforce passing a label to every class defined in your codebase, you can set `autofillLabels` to `false` at the start of app:
|
|
438
|
-
|
|
439
|
-
```ts
|
|
440
|
-
import { updateSigilOptions } from '@vicin/sigil';
|
|
441
|
-
|
|
442
|
-
updateSigilOptions({ autofillLabels: false });
|
|
443
|
-
```
|
|
444
|
-
|
|
445
|
-
Now if you forgot to pass a label error is thrown at the moment you create class instance or use any of `Sigil` methods.
|
|
446
|
-
|
|
447
|
-
---
|
|
399
|
+
## Edge cases
|
|
448
400
|
|
|
449
|
-
|
|
401
|
+
### Attach sigil to parent after child
|
|
450
402
|
|
|
451
|
-
|
|
452
|
-
To avoid this you can set global options to skip label uniqueness check at the start of app:
|
|
403
|
+
When attaching `Sigil` labels to classes order from parent -> child must be respected
|
|
453
404
|
|
|
454
405
|
```ts
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
updateSigilOptions({ skipLabelUniquenessCheck: true });
|
|
458
|
-
```
|
|
459
|
-
|
|
460
|
-
But this can cause unexpected behavior if same label is used for two different classes as checks are disabled globally.
|
|
461
|
-
If you need more strict mode you can pass this options to the re-loaded class only:
|
|
406
|
+
class Parent extends Sigil {}
|
|
407
|
+
class Child extends Parent {}
|
|
462
408
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
class HmrClass extends Sigil {}
|
|
409
|
+
attachSigil(Child, 'Child');
|
|
410
|
+
attachSigil(Parent, 'Parent'); // Throws: [Sigil Error] Class 'Parent' with label 'Sigil' is already sigilified
|
|
466
411
|
```
|
|
467
412
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
---
|
|
471
|
-
|
|
472
|
-
## Edge cases
|
|
473
|
-
|
|
474
|
-
### Accessing Sigil `metadata` before running 'attachSigil' function
|
|
413
|
+
### Accessing Sigil `metadata` before running `attachSigil` function
|
|
475
414
|
|
|
476
415
|
If you didn't make sure that `attachSigil` runs right after class declaration and used one of `Sigil` methods this will occur:
|
|
477
416
|
|
|
478
417
|
```ts
|
|
479
418
|
class A extends Sigil {}
|
|
480
419
|
|
|
481
|
-
console.log(A.SigilLabel); // returns
|
|
420
|
+
console.log(A.SigilLabel); // returns label of the parent (Sigil in our case)
|
|
482
421
|
|
|
483
422
|
attachSigil(A, 'A');
|
|
484
423
|
|
|
@@ -500,24 +439,21 @@ Note that you can't use `InstanceType` on `private` or `protected` classes, howe
|
|
|
500
439
|
|
|
501
440
|
#### Static blocks & IIFE static initializer
|
|
502
441
|
|
|
503
|
-
|
|
442
|
+
Stage 3 decorators (`AttachSigil`) and function (`attachSigil`) runs after IIFE and static blocks, so accessing `Sigil` metadata inside them should be avoided
|
|
504
443
|
|
|
505
444
|
```ts
|
|
445
|
+
@AttachSigil('A')
|
|
506
446
|
class A extends Sigil {
|
|
507
447
|
static IIFE = (() => {
|
|
508
|
-
const label = A.SigilLabel; // returns
|
|
448
|
+
const label = A.SigilLabel; // returns label of the parent (Sigil in our case)
|
|
509
449
|
})();
|
|
510
450
|
|
|
511
451
|
static {
|
|
512
|
-
const label = this.SigilLabel; // returns
|
|
452
|
+
const label = this.SigilLabel; // returns label of the parent (Sigil in our case)
|
|
513
453
|
}
|
|
514
454
|
}
|
|
515
|
-
|
|
516
|
-
attachSigil(A, 'A');
|
|
517
455
|
```
|
|
518
456
|
|
|
519
|
-
This behavior can't be avoided, so make sure not to call any `Sigil` method inside them or move to decorators (`@AttachSigil`)
|
|
520
|
-
|
|
521
457
|
---
|
|
522
458
|
|
|
523
459
|
## Benchmarks
|
|
@@ -526,68 +462,95 @@ Sigil is built for **real-world performance**. Below are the latest micro-benchm
|
|
|
526
462
|
|
|
527
463
|
**Running Tests**
|
|
528
464
|
|
|
529
|
-
To run benchmarks on your machine fetch source code from [github](https://github.com/ZiadTaha62/
|
|
465
|
+
To run benchmarks on your machine fetch source code from [github](https://github.com/ZiadTaha62/vicin-packages) then:
|
|
530
466
|
|
|
531
467
|
```bash
|
|
532
|
-
|
|
533
|
-
|
|
468
|
+
pnpm install
|
|
469
|
+
pnpm run bench --filter @vicin/sigil
|
|
534
470
|
```
|
|
535
471
|
|
|
536
472
|
### 1. Runtime Type Checking
|
|
537
473
|
|
|
538
|
-
|
|
|
539
|
-
|
|
|
540
|
-
|
|
|
541
|
-
|
|
|
542
|
-
|
|
|
543
|
-
|
|
|
544
|
-
|
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
474
|
+
| Test Name | Ops/sec (hz) | Mean (ms) | p99 (ms) | RME | Samples |
|
|
475
|
+
| -------------------------- | ------------ | --------- | -------- | ------ | --------- |
|
|
476
|
+
| **Depth 0** | | | | | |
|
|
477
|
+
| instanceof | 11,986,628 | 0.0001 | 0.0002 | ±0.58% | 5,993,314 |
|
|
478
|
+
| isInstance (Ctor) | 8,848,040 | 0.0001 | 0.0003 | ±0.63% | 4,424,020 |
|
|
479
|
+
| isInstance (instance) | 9,246,878 | 0.0001 | 0.0002 | ±0.86% | 4,623,440 |
|
|
480
|
+
| isExactInstance (Ctor) | 5,114,916 | 0.0002 | 0.0003 | ±0.44% | 2,557,459 |
|
|
481
|
+
| isExactInstance (instance) | 5,553,370 | 0.0002 | 0.0003 | ±0.41% | 2,776,686 |
|
|
482
|
+
| **Depth 5** | | | | | |
|
|
483
|
+
| instanceof | 8,399,150 | 0.0001 | 0.0002 | ±0.48% | 4,199,651 |
|
|
484
|
+
| isInstance (Ctor) | 7,385,890 | 0.0001 | 0.0003 | ±2.01% | 3,692,945 |
|
|
485
|
+
| isInstance (instance) | 7,862,474 | 0.0001 | 0.0003 | ±0.77% | 3,931,238 |
|
|
486
|
+
| isExactInstance (Ctor) | 4,433,700 | 0.0002 | 0.0004 | ±0.64% | 2,216,851 |
|
|
487
|
+
| isExactInstance (instance) | 4,898,498 | 0.0002 | 0.0004 | ±0.34% | 2,449,249 |
|
|
488
|
+
| **Depth 10** | | | | | |
|
|
489
|
+
| instanceof | 7,144,486 | 0.0001 | 0.0003 | ±0.63% | 3,572,243 |
|
|
490
|
+
| isInstance (Ctor) | 7,165,920 | 0.0001 | 0.0003 | ±0.49% | 3,582,960 |
|
|
491
|
+
| isInstance (instance) | 7,834,056 | 0.0001 | 0.0003 | ±1.16% | 3,917,029 |
|
|
492
|
+
| isExactInstance (Ctor) | 4,292,578 | 0.0002 | 0.0004 | ±0.34% | 2,146,290 |
|
|
493
|
+
| isExactInstance (instance) | 5,020,923 | 0.0002 | 0.0004 | ±0.28% | 2,510,462 |
|
|
494
|
+
| **Depth 15** | | | | | |
|
|
495
|
+
| instanceof | 7,116,266 | 0.0001 | 0.0002 | ±0.38% | 3,558,134 |
|
|
496
|
+
| isInstance (Ctor) | 6,308,498 | 0.0002 | 0.0003 | ±0.41% | 3,154,249 |
|
|
497
|
+
| isInstance (instance) | 6,403,126 | 0.0002 | 0.0003 | ±0.70% | 3,201,564 |
|
|
498
|
+
| isExactInstance (Ctor) | 3,678,280 | 0.0003 | 0.0005 | ±0.37% | 1,839,141 |
|
|
499
|
+
| isExactInstance (instance) | 3,753,618 | 0.0003 | 0.0005 | ±0.39% | 1,876,810 |
|
|
500
|
+
|
|
501
|
+
> **Key takeaway**:
|
|
502
|
+
>
|
|
503
|
+
> `isInstance` Efficiency: Demonstrates high parity with native instanceof, maintaining a minimal performance delta (approx. 15% overhead).
|
|
504
|
+
> `isExactInstance` Cost-Benefit: While introducing a ~2x overhead compared to native operations, it remains performant in absolute terms. The throughput cost is a deliberate trade-off for the increased precision required for exact matching.
|
|
548
505
|
|
|
549
506
|
### 2. Class Definition & Instance Creation
|
|
550
507
|
|
|
551
|
-
|
|
|
552
|
-
|
|
|
553
|
-
|
|
|
554
|
-
| Empty Sigil
|
|
555
|
-
| Small (
|
|
556
|
-
| Large (
|
|
557
|
-
|
|
|
558
|
-
|
|
|
559
|
-
|
|
|
560
|
-
|
|
|
561
|
-
|
|
|
508
|
+
| Test Name | Ops/sec (hz) | Mean (ms) | p99 (ms) | RME | Samples |
|
|
509
|
+
| ------------------------------------- | ----------------- | ---------------- | ---------------- | ------- | --------- |
|
|
510
|
+
| **Definition (Module Load Time)** | | | | | |
|
|
511
|
+
| Define Empty (Plain vs Sigil) | 122,640 vs 13,154 | 0.0082 vs 0.0760 | 0.0206 vs 0.1413 | ±11.36% | 6,578 |
|
|
512
|
+
| Define Small (Plain vs Sigil) | 75,363 vs 12,003 | 0.0133 vs 0.0833 | 0.0267 vs 0.1596 | ±5.98% | 6,002 |
|
|
513
|
+
| Define Large (Plain vs Sigil) | 53,392 vs 11,978 | 0.0187 vs 0.0835 | 0.0352 vs 0.1471 | ±6.13% | 5,990 |
|
|
514
|
+
| Define Depth 3 (Plain vs Sigil) | 22,802 vs 5,205 | 0.0439 vs 0.1921 | 0.0850 vs 0.3139 | ±14.68% | 2,603 |
|
|
515
|
+
| Define Depth 5 (Plain vs Sigil) | 12,003 vs 3,369 | 0.0833 vs 0.2968 | 0.1864 vs 0.5488 | ±7.31% | 1,685 |
|
|
516
|
+
| Define Depth 10 (Plain vs Sigil) | 5,477 vs 1,758 | 0.1826 vs 0.5685 | 0.3402 vs 1.1148 | ±9.62% | 880 |
|
|
517
|
+
| **Instantiation (Runtime/Hot Path)** | | | | | |
|
|
518
|
+
| Instantiate Empty (Plain vs Sigil) | 12.0M vs 11.9M | 0.0001 vs 0.0001 | 0.0002 vs 0.0002 | ±0.76% | 5,999,555 |
|
|
519
|
+
| Instantiate Small (Plain vs Sigil) | 12.5M vs 11.4M | 0.0001 vs 0.0001 | 0.0001 vs 0.0002 | ±0.89% | 5,739,986 |
|
|
520
|
+
| Instantiate Large (Plain vs Sigil) | 12.4M vs 12.4M | 0.0001 vs 0.0001 | 0.0002 vs 0.0001 | ±0.74% | 6,205,306 |
|
|
521
|
+
| Instantiate Depth 3 (Plain vs Sigil) | 12.0M vs 12.1M | 0.0001 vs 0.0001 | 0.0002 vs 0.0002 | ±1.01% | 6,092,517 |
|
|
522
|
+
| Instantiate Depth 5 (Plain vs Sigil) | 9.09M vs 8.54M | 0.0001 vs 0.0001 | 0.0002 vs 0.0003 | ±2.24% | 4,272,328 |
|
|
523
|
+
| Instantiate Depth 10 (Plain vs Sigil) | 2.87M vs 1.72M | 0.0003 vs 0.0006 | 0.0008 vs 0.0011 | ±0.75% | 862,564 |
|
|
562
524
|
|
|
563
525
|
> **Key takeaways**:
|
|
564
526
|
>
|
|
565
|
-
> -
|
|
566
|
-
>
|
|
527
|
+
> Front-Loaded Definition Overhead: Sigil introduces main overhead during the class definition phase (roughly 6x to 10x slower than plain classes). This is a one-time cost per class, typically occurring during module evaluation or registry setup.
|
|
528
|
+
>
|
|
529
|
+
> Runtime Performance Parity: Once the class is defined, Sigil achieves near-native instantiation throughput. For standard object creation, the delta is negligible, ensuring that the library does not bottleneck high-frequency allocation patterns.
|
|
530
|
+
>
|
|
531
|
+
> Scale Stability: The overhead of Sigil remains constant regardless of the number of properties or methods added (Small vs. Large). The definition speed for a "Small Sigil" and a "Large Sigil" is nearly identical (~12k hz), suggesting that the setup logic is O(1) relative to class member count.
|
|
567
532
|
|
|
568
533
|
---
|
|
569
534
|
|
|
570
535
|
## Bundle Size
|
|
571
536
|
|
|
572
|
-
**Less than 1
|
|
537
|
+
**Less than 1 KB (908 B)** (minified + Brotli, including all dependencies)
|
|
573
538
|
|
|
574
539
|
This makes Sigil one of the smallest full-featured solutions for nominal typing + reliable runtime identity.
|
|
575
540
|
|
|
576
541
|
**Running Tests**
|
|
577
542
|
|
|
578
|
-
To verify bundle size fetch source code from [github](https://github.com/ZiadTaha62/
|
|
543
|
+
To verify bundle size fetch source code from [github](https://github.com/ZiadTaha62/vicin-packages) then:
|
|
579
544
|
|
|
580
545
|
```bash
|
|
581
|
-
|
|
582
|
-
|
|
546
|
+
pnpm install
|
|
547
|
+
pnpm run size --filter @vicin/sigil
|
|
583
548
|
```
|
|
584
549
|
|
|
585
550
|
---
|
|
586
551
|
|
|
587
552
|
## Tests
|
|
588
553
|
|
|
589
|
-
Reliability is a core pillar of `Sigil`. The library is backed by a comprehensive suite of unit tests ( 71 total tests ) that cover everything from basic mixins to edge cases.
|
|
590
|
-
|
|
591
554
|
**Coverage Status**
|
|
592
555
|
|
|
593
556
|
We maintain **100%** test coverage across the entire codebase to ensure that runtime metadata remains consistent and predictable.
|
|
@@ -599,22 +562,13 @@ We maintain **100%** test coverage across the entire codebase to ensure that run
|
|
|
599
562
|
| Funcs | 100% |
|
|
600
563
|
| Lines | 100% |
|
|
601
564
|
|
|
602
|
-
**Key Test Areas**
|
|
603
|
-
|
|
604
|
-
- **Mixins, Attach function & decorator:** Validating `Sigilify`, `AttachSigil` and `attachSigil` behaviors.
|
|
605
|
-
- **Sigil methods:** Ensuring `Sigil` class methods (e.g. `SigilLabel`, `getSigilLabel`) work as expected.
|
|
606
|
-
- **Lazy Evaluation:** Ensuring metadata is attached before being accessed via `Sigil` methods even when no attach function or decorator is used.
|
|
607
|
-
- **Lineage:** Verifying that `isOfType` and `isExactType` work across complex inheritance chains.
|
|
608
|
-
- **Error Handling:** Strict validation for all errors and throws.
|
|
609
|
-
- **Edge cases**: Known edge cases.
|
|
610
|
-
|
|
611
565
|
**Running Tests**
|
|
612
566
|
|
|
613
|
-
To run the test suite locally and generate a coverage report, fetch source code from [github](https://github.com/ZiadTaha62/
|
|
567
|
+
To run the test suite locally and generate a coverage report, fetch source code from [github](https://github.com/ZiadTaha62/vicin-packages) then:
|
|
614
568
|
|
|
615
569
|
```bash
|
|
616
|
-
|
|
617
|
-
|
|
570
|
+
pnpm install
|
|
571
|
+
pnpm run test --filter @vicin/sigil
|
|
618
572
|
```
|
|
619
573
|
|
|
620
574
|
---
|