@naturalcycles/js-lib 15.9.1 → 15.10.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.
@@ -1,5 +1,5 @@
1
1
  import type { MutateOptions } from '../array/array.util.js';
2
- import type { AnyObject, KeyValueTuple, ObjectMapper, ObjectPredicate, Reviver, ValueOf } from '../types.js';
2
+ import type { AnyObject, KeyValueTuple, ObjectMapper, ObjectPredicate, Reviver, StringMap, ValueOf } from '../types.js';
3
3
  import { SKIP } from '../types.js';
4
4
  /**
5
5
  * Returns clone of `obj` with only `props` preserved.
@@ -93,6 +93,18 @@ export declare function _objectNullValuesToUndefined<T extends AnyObject>(obj: T
93
93
  * Deep copy object (by json parse/stringify, since it has unbeatable performance+simplicity combo).
94
94
  */
95
95
  export declare function _deepCopy<T>(o: T, reviver?: Reviver): T;
96
+ /**
97
+ * Performance-optimized implementation of merging two objects
98
+ * without mutating any of them.
99
+ * (if you are allowed to mutate - there can be a faster implementation).
100
+ *
101
+ * Gives ~40% speedup with map sizes between 10 and 100k items,
102
+ * compared to {...obj1, ...obj2} or Object.assign({}, obj1, obj2).
103
+ *
104
+ * Only use it in hot paths that are known to be performance bottlenecks,
105
+ * otherwise it's not worth it (use normal object spread then).
106
+ */
107
+ export declare function _mergeObjects<T>(obj1: StringMap<T>, obj2: StringMap<T>): StringMap<T>;
96
108
  /**
97
109
  * Returns `undefined` if it's empty (according to `_isEmpty()` specification),
98
110
  * otherwise returns the original object.
@@ -192,6 +192,25 @@ export function _objectNullValuesToUndefined(obj, opt = {}) {
192
192
  export function _deepCopy(o, reviver) {
193
193
  return JSON.parse(JSON.stringify(o), reviver);
194
194
  }
195
+ /**
196
+ * Performance-optimized implementation of merging two objects
197
+ * without mutating any of them.
198
+ * (if you are allowed to mutate - there can be a faster implementation).
199
+ *
200
+ * Gives ~40% speedup with map sizes between 10 and 100k items,
201
+ * compared to {...obj1, ...obj2} or Object.assign({}, obj1, obj2).
202
+ *
203
+ * Only use it in hot paths that are known to be performance bottlenecks,
204
+ * otherwise it's not worth it (use normal object spread then).
205
+ */
206
+ export function _mergeObjects(obj1, obj2) {
207
+ const map = {};
208
+ for (const k of Object.keys(obj1))
209
+ map[k] = obj1[k];
210
+ for (const k of Object.keys(obj2))
211
+ map[k] = obj2[k];
212
+ return map;
213
+ }
195
214
  /**
196
215
  * Returns `undefined` if it's empty (according to `_isEmpty()` specification),
197
216
  * otherwise returns the original object.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.9.1",
4
+ "version": "15.10.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2",
7
7
  "zod": "^3"
@@ -6,6 +6,7 @@ import type {
6
6
  ObjectMapper,
7
7
  ObjectPredicate,
8
8
  Reviver,
9
+ StringMap,
9
10
  ValueOf,
10
11
  } from '../types.js'
11
12
  import { _objectEntries, SKIP } from '../types.js'
@@ -246,6 +247,24 @@ export function _deepCopy<T>(o: T, reviver?: Reviver): T {
246
247
  return JSON.parse(JSON.stringify(o), reviver)
247
248
  }
248
249
 
250
+ /**
251
+ * Performance-optimized implementation of merging two objects
252
+ * without mutating any of them.
253
+ * (if you are allowed to mutate - there can be a faster implementation).
254
+ *
255
+ * Gives ~40% speedup with map sizes between 10 and 100k items,
256
+ * compared to {...obj1, ...obj2} or Object.assign({}, obj1, obj2).
257
+ *
258
+ * Only use it in hot paths that are known to be performance bottlenecks,
259
+ * otherwise it's not worth it (use normal object spread then).
260
+ */
261
+ export function _mergeObjects<T>(obj1: StringMap<T>, obj2: StringMap<T>): StringMap<T> {
262
+ const map: StringMap<T> = {}
263
+ for (const k of Object.keys(obj1)) map[k] = obj1[k]
264
+ for (const k of Object.keys(obj2)) map[k] = obj2[k]
265
+ return map
266
+ }
267
+
249
268
  /**
250
269
  * Returns `undefined` if it's empty (according to `_isEmpty()` specification),
251
270
  * otherwise returns the original object.