Skip to content

Commit e519ac7

Browse files
author
Steve Thompson
committed
Trying new ways to get class to instantiate faster.
1 parent 7b22dd0 commit e519ac7

File tree

7 files changed

+173
-107
lines changed

7 files changed

+173
-107
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,17 +569,17 @@ runMethod_and_returnThis(
569569

570570
```
571571
// changing the array content:
572-
arr.data = [{prop1: 'yes', prop2: 'no'}, {prop1: 'no', prop2: 'yes'}];
572+
arr.data = [item1, item2, item3, ...];
573573
574574
// changing the array content without breaking its memory reference:
575-
arr.set( [ {prop1: 'yes', prop2: 'no'}, {prop1: 'no', prop2: 'yes'} ] );
575+
arr.set( [item1, item2, item3, ...] );
576576
```
577577

578578
## Performance
579579

580580
PublicArray has a large number of dependencies. You should keep this in mind when optimizing
581581
the performance of your app. For example, say your code only uses PublicArray's `.replace` property
582-
and nothing else. Since `.replace` is an instance of PublicArrayReplacer, you will get a slight
582+
and nothing else. Since `.replace` is an instance of PublicArrayReplacer, you will get a
583583
performance boost if you just instantiate PublicArrayReplacer instead of PublicArray:
584584
```
585585
let replace = new PublicArrayReplacer(array);

dist/privy/PublicArray.d.ts

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1-
import { PublicArrayRemover } from '@writetome51/public-array-remover';
21
import { PublicArrayContent } from '@writetome51/public-array-content';
3-
import { PublicArrayGetter } from '@writetome51/public-array-getter';
4-
import { PublicArrayInserter } from '@writetome51/public-array-inserter';
5-
import { PublicArraySorter } from '@writetome51/public-array-sorter';
6-
import { PublicArrayReplacer } from '@writetome51/public-array-replacer';
7-
import { PublicArrayGetterConverter } from '@writetome51/public-array-getter-converter';
8-
import { PublicArrayGetterRemover } from '@writetome51/public-array-getter-remover';
9-
import { PublicArrayFilter } from '@writetome51/public-array-filter';
102

113

12-
/**************
13-
This class is called PublicArray because an array is contained inside it,
14-
in a public property: 'data'
4+
/*********************
5+
These imports are commented out to help improve performance.
6+
They're still kept here as a record of the npm module each comes from.
7+
8+
// import { PublicArrayRemover } from '@writetome51/public-array-remover';
9+
// import { PublicArrayGetter } from '@writetome51/public-array-getter';
10+
// import { PublicArrayInserter } from '@writetome51/public-array-inserter';
11+
// import { PublicArraySorter } from '@writetome51/public-array-sorter';
12+
// import { PublicArrayReplacer } from '@writetome51/public-array-replacer';
13+
// import { PublicArrayGetterConverter } from '@writetome51/public-array-getter-converter';
14+
// import { PublicArrayGetterRemover } from '@writetome51/public-array-getter-remover';
15+
// import { PublicArrayFilter } from '@writetome51/public-array-filter';
16+
********************/
17+
/***********************
18+
This class is for general array manipulation. It's called PublicArray because it
19+
contains an array in a public property: 'data' .
1520
1621
The main reason you would use this class is if you hate JavaScript's built-in Array
1722
methods, like .slice(), .splice(), .push(), and .shift(). This class has much clearer
18-
and expressive method names.
23+
and expressive method names, and a lot more of them.
1924
2025
A few examples of usage:
2126
22-
let arr = getPublicArray( [1,2,3,4,5,6] );
27+
let arr = getPublicArray([1,2,3,4,5,6]);
2328
arr.remove.tail(2); // arr.data is now [1,2,3,4]
2429
if (arr.notEmpty) arr.prepend([10]); // arr.data is now [10,1,2,3,4]
25-
*************/
26-
27-
30+
**********************/
2831
export declare class PublicArray extends PublicArrayContent {
2932

3033
private _filter;
@@ -39,32 +42,29 @@ export declare class PublicArray extends PublicArrayContent {
3942

4043
// Public Properties:
4144

42-
readonly copy: PublicArray; // independent copy of this instance.
43-
readonly filter: PublicArrayFilter;
44-
readonly getConverted: PublicArrayGetterConverter;
45-
readonly get: PublicArrayGetter;
46-
readonly getAndRemove: PublicArrayGetterRemover;
47-
readonly insert: PublicArrayInserter;
48-
readonly remove: PublicArrayRemover;
49-
readonly replace: PublicArrayReplacer;
50-
readonly sort: PublicArraySorter;
51-
52-
53-
constructor(
54-
// begin injected dependencies...
55-
_filter: PublicArrayFilter,
56-
_getConverted: PublicArrayGetterConverter,
57-
_get: PublicArrayGetter,
58-
_getAndRemove: PublicArrayGetterRemover,
59-
_insert: PublicArrayInserter,
60-
_remove: PublicArrayRemover,
61-
_replace: PublicArrayReplacer,
62-
_sort: PublicArraySorter,
63-
// ... end injected dependencies
64-
65-
// the actual array:
66-
data?: any[]
67-
);
45+
// readonly copy: PublicArray (an independent copy of this instance).
46+
// readonly filter: PublicArrayFilter;
47+
// readonly getConverted: PublicArrayGetterConverter;
48+
// readonly get: PublicArrayGetter;
49+
// readonly getAndRemove: PublicArrayGetterRemover;
50+
// readonly insert: PublicArrayInserter;
51+
// readonly remove: PublicArrayRemover;
52+
// readonly replace: PublicArrayReplacer;
53+
// readonly sort: PublicArraySorter;
54+
55+
56+
constructor(_filter: any, // PublicArrayFilter,
57+
_getConverted: any, // PublicArrayGetterConverter,
58+
_get: any, // PublicArrayGetter,
59+
_getAndRemove: any, // PublicArrayGetterRemover,
60+
_insert: any, // PublicArrayInserter,
61+
_remove: any, // PublicArrayRemover,
62+
_replace: any, // PublicArrayReplacer,
63+
_sort: any, // PublicArraySorter,
64+
data?: any[]);
65+
66+
67+
readonly copy: PublicArray;
6868

6969

7070
append(values: any[]): this;
@@ -73,11 +73,8 @@ export declare class PublicArray extends PublicArrayContent {
7373
prepend(values: any[]): this;
7474

7575

76-
forEach(
77-
iterationFunction: (currentValue: any, currentIndex?: number, entireArray?: any[]) => any
78-
): this;
76+
forEach(iterationFunction: (currentValue: any, currentIndex?: number, entireArray?: any[]) => any): this;
7977

8078

8179
set(newArray: any[]): this;
82-
8380
}

dist/privy/PublicArray.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ var array_append_prepend_1 = require("@writetome51/array-append-prepend");
1717
var set_array_1 = require("@writetome51/set-array");
1818
var di_factory_1 = require("@writetome51/di-factory");
1919
var public_array_content_1 = require("@writetome51/public-array-content");
20+
/*********************
21+
These imports are commented out to help improve performance.
22+
They're still kept here as a record of the npm module each comes from.
23+
2024
// import { PublicArrayRemover } from '@writetome51/public-array-remover';
2125
// import { PublicArrayGetter } from '@writetome51/public-array-getter';
2226
// import { PublicArrayInserter } from '@writetome51/public-array-inserter';
@@ -25,23 +29,24 @@ var public_array_content_1 = require("@writetome51/public-array-content");
2529
// import { PublicArrayGetterConverter } from '@writetome51/public-array-getter-converter';
2630
// import { PublicArrayGetterRemover } from '@writetome51/public-array-getter-remover';
2731
// import { PublicArrayFilter } from '@writetome51/public-array-filter';
28-
/**************
29-
This class is called PublicArray because an array is contained inside it,
30-
in a public property: 'data'
32+
********************/
33+
/***********************
34+
This class is for general array manipulation. It's called PublicArray because it
35+
contains an array in a public property: 'data' .
3136
3237
The main reason you would use this class is if you hate JavaScript's built-in Array
3338
methods, like .slice(), .splice(), .push(), and .shift(). This class has much clearer
34-
and expressive method names.
39+
and expressive method names, and a lot more of them.
3540
3641
A few examples of usage:
3742
38-
let arr = getPublicArray( [1,2,3,4,5,6] );
43+
let arr = getPublicArray([1,2,3,4,5,6]);
3944
arr.remove.tail(2); // arr.data is now [1,2,3,4]
4045
if (arr.notEmpty) arr.prepend([10]); // arr.data is now [10,1,2,3,4]
41-
*************/
46+
**********************/
4247
var PublicArray = /** @class */ (function (_super) {
4348
__extends(PublicArray, _super);
44-
//Public Properties:
49+
// Public Properties:
4550
// readonly copy: PublicArray (an independent copy of this instance).
4651
// readonly filter: PublicArrayFilter;
4752
// readonly getConverted: PublicArrayGetterConverter;
@@ -53,14 +58,14 @@ var PublicArray = /** @class */ (function (_super) {
5358
// readonly sort: PublicArraySorter;
5459
function PublicArray(
5560
// begin injected dependencies...
56-
_filter, //: PublicArrayFilter,
57-
_getConverted, //: PublicArrayGetterConverter,
58-
_get, //: PublicArrayGetter,
59-
_getAndRemove, //: PublicArrayGetterRemover,
60-
_insert, //: PublicArrayInserter,
61-
_remove, //: PublicArrayRemover,
62-
_replace, //: PublicArrayReplacer,
63-
_sort, //: PublicArraySorter,
61+
_filter, // PublicArrayFilter,
62+
_getConverted, // PublicArrayGetterConverter,
63+
_get, // PublicArrayGetter,
64+
_getAndRemove, // PublicArrayGetterRemover,
65+
_insert, // PublicArrayInserter,
66+
_remove, // PublicArrayRemover,
67+
_replace, // PublicArrayReplacer,
68+
_sort, // PublicArraySorter,
6469
// ... end injected dependencies
6570
data // the actual array, represented by inherited property this.data
6671
) {
@@ -88,7 +93,7 @@ var PublicArray = /** @class */ (function (_super) {
8893
return _this;
8994
}
9095
Object.defineProperty(PublicArray.prototype, "copy", {
91-
// this.copy -- returns independent copy of this.
96+
// this.copy -- returns independent copy of 'this', not linked to 'this' in any way.
9297
get: function () {
9398
// @ts-ignore
9499
return di_factory_1.DIFactory.getInstance(PublicArray, [this.get.copy()]);
@@ -105,7 +110,7 @@ var PublicArray = /** @class */ (function (_super) {
105110
PublicArray.prototype.forEach = function (iterationFunction) {
106111
return this.returnThis_after(this.data.forEach(iterationFunction));
107112
};
108-
// Use this for changing value of this.data without breaking its memory reference.
113+
// Use this to change the value of this.data without breaking its memory reference.
109114
PublicArray.prototype.set = function (newArray) {
110115
return this.returnThis_after(set_array_1.setArray(this.data, newArray));
111116
};

dist/tests.d.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
export {};
2+
/*******************
3+
let arr = getPublicArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
4+
5+
// Test 1: getPublicArray() must return instance of PublicArray:
6+
if (arr.className && arr.className === 'PublicArray') console.log('test 1 passed');
7+
else console.log('test 1 FAILED');
8+
9+
// Test 1A: the instance must contain the array passed into getPublicArray():
10+
if (arraysMatch(arr.data, [1, 2, 3, 4, 5, 6, 7, 8, 9])) console.log('test 1A passed');
11+
else console.log('test 1A FAILED');
12+
13+
// Test 2: :
14+
if (arr.className && arr.className === 'PublicArray') console.log('test 2 passed');
15+
else console.log('test 2 FAILED');
16+
17+
// Test 2A: the instance must contain the array passed into getPublicArray():
18+
if (arraysMatch(arr.data, [1, 2, 3, 4, 5, 6, 7, 8, 9])) console.log('test 1A passed');
19+
else console.log('test 1A FAILED');
20+
*******************/
21+
/*********
22+
console.log(
23+
arr.remove
24+
.byIndexes([0, 2, 4, 6])
25+
.data
26+
);// [2, 4, 6, 8, 9]
27+
************/
228
/****************
329
430
5-
let otherArr = arr.data;
31+
let otherArr = arr.data;
632
7-
otherArr.length = 0;
33+
otherArr.length = 0;
834
9-
console.log(otherArr);
35+
console.log(otherArr);
1036
11-
console.log(arr.data);
37+
console.log(arr.data);
1238
*************/
1339
/************
1440
arr.remove.allAfterFirst(6);

dist/tests.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
var index_1 = require("./index");
4-
var arrays_match_1 = require("@writetome51/arrays-match");
4+
/*******************
5+
let arr: any = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
6+
arr.splice(10, 0, [1]);
7+
console.log(arr);
8+
*****************/
59
var arr = index_1.getPublicArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
610
// Test 1: getPublicArray() must return instance of PublicArray:
711
if (arr.className && arr.className === 'PublicArray')
812
console.log('test 1 passed');
913
else
1014
console.log('test 1 FAILED');
11-
// Test 1A: the instance must contain the array passed into getPublicArray():
12-
if (arrays_match_1.arraysMatch(arr.data, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
13-
console.log('test 1A passed');
14-
else
15-
console.log('test 1A FAILED');
16-
console.log(arr.remove
17-
.byIndexes([0, 2, 4, 6])
18-
.data); // [2, 4, 6, 8, 9]
15+
/*******************
16+
// Test 1A: the instance must contain the array passed into getPublicArray():
17+
if (arraysMatch(arr.data, [1, 2, 3, 4, 5, 6, 7, 8, 9])) console.log('test 1A passed');
18+
else console.log('test 1A FAILED');
19+
20+
// Test 2: :
21+
if (arr.className && arr.className === 'PublicArray') console.log('test 2 passed');
22+
else console.log('test 2 FAILED');
23+
24+
// Test 2A: the instance must contain the array passed into getPublicArray():
25+
if (arraysMatch(arr.data, [1, 2, 3, 4, 5, 6, 7, 8, 9])) console.log('test 1A passed');
26+
else console.log('test 1A FAILED');
27+
*******************/
28+
/*********
29+
console.log(
30+
arr.remove
31+
.byIndexes([0, 2, 4, 6])
32+
.data
33+
);// [2, 4, 6, 8, 9]
34+
************/
1935
/****************
2036
2137

0 commit comments

Comments
 (0)