msgpackr 1.9.5 → 1.9.7

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/dist/test.js CHANGED
@@ -1319,25 +1319,9 @@
1319
1319
  return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
1320
1320
  } finally {
1321
1321
  if (structures) {
1322
- if (serializationsSinceTransitionRebuild < 10)
1323
- serializationsSinceTransitionRebuild++;
1324
- let sharedLength = structures.sharedLength || 0;
1325
- if (structures.length > sharedLength && !isSequential)
1326
- structures.length = sharedLength;
1327
- if (transitionsCount > 10000) {
1328
- // force a rebuild occasionally after a lot of transitions so it can get cleaned up
1329
- structures.transitions = null;
1330
- serializationsSinceTransitionRebuild = 0;
1331
- transitionsCount = 0;
1332
- if (recordIdsToRemove.length > 0)
1333
- recordIdsToRemove = [];
1334
- } else if (recordIdsToRemove.length > 0 && !isSequential) {
1335
- for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
1336
- recordIdsToRemove[i][RECORD_SYMBOL] = 0;
1337
- }
1338
- recordIdsToRemove = [];
1339
- }
1322
+ resetStructures();
1340
1323
  if (hasSharedUpdate && packr.saveStructures) {
1324
+ let sharedLength = structures.sharedLength || 0;
1341
1325
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1342
1326
  let returnBuffer = target.subarray(start, position);
1343
1327
  let newSharedData = prepareStructures$1(structures, packr);
@@ -1353,6 +1337,26 @@
1353
1337
  position = start;
1354
1338
  }
1355
1339
  };
1340
+ const resetStructures = () => {
1341
+ if (serializationsSinceTransitionRebuild < 10)
1342
+ serializationsSinceTransitionRebuild++;
1343
+ let sharedLength = structures.sharedLength || 0;
1344
+ if (structures.length > sharedLength && !isSequential)
1345
+ structures.length = sharedLength;
1346
+ if (transitionsCount > 10000) {
1347
+ // force a rebuild occasionally after a lot of transitions so it can get cleaned up
1348
+ structures.transitions = null;
1349
+ serializationsSinceTransitionRebuild = 0;
1350
+ transitionsCount = 0;
1351
+ if (recordIdsToRemove.length > 0)
1352
+ recordIdsToRemove = [];
1353
+ } else if (recordIdsToRemove.length > 0 && !isSequential) {
1354
+ for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
1355
+ recordIdsToRemove[i][RECORD_SYMBOL] = 0;
1356
+ }
1357
+ recordIdsToRemove = [];
1358
+ }
1359
+ };
1356
1360
  const packArray = (value) => {
1357
1361
  var length = value.length;
1358
1362
  if (length < 0x10) {
@@ -1802,8 +1806,9 @@
1802
1806
  }
1803
1807
  // now write the values
1804
1808
  for (let key in object)
1805
- if (safePrototype || object.hasOwnProperty(key))
1809
+ if (safePrototype || object.hasOwnProperty(key)) {
1806
1810
  pack(object[key]);
1811
+ }
1807
1812
  };
1808
1813
 
1809
1814
  // craete reference to useRecords if useRecords is a function
@@ -1922,6 +1927,7 @@
1922
1927
  position = newPosition;
1923
1928
  let startTarget = target;
1924
1929
  pack(value);
1930
+ resetStructures();
1925
1931
  if (startTarget !== target) {
1926
1932
  return { position, targetView, target }; // indicate the buffer was re-allocated
1927
1933
  }
@@ -2278,25 +2284,28 @@
2278
2284
  switch (typeof value) {
2279
2285
  case 'number':
2280
2286
  let number = value;
2281
- if (number >> 0 === number && number < 0x20000000 && number > -0x1f000000) {
2282
- if (number < 0xf6 && number >= 0 && (nextTransition.num8 || number < 0x20 && !nextTransition.num32)) {
2283
- transition = nextTransition.num8 || createTypeTransition(nextTransition, NUMBER, 1);
2284
- target[position++] = number;
2285
- } else {
2286
- transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
2287
- targetView.setUint32(position, number, true);
2288
- position += 4;
2289
- }
2290
- break;
2291
- } else if (number < 0x100000000 && number >= -0x80000000) {
2292
- targetView.setFloat32(position, number, true);
2293
- if (float32Headers[target[position + 3] >>> 5]) {
2294
- let xShifted;
2295
- // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
2296
- if (((xShifted = number * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
2287
+ // first check to see if we are using a lot of ids and should default to wide/common format
2288
+ if (nextId < 200 || !nextTransition.num64) {
2289
+ if (number >> 0 === number && number < 0x20000000 && number > -0x1f000000) {
2290
+ if (number < 0xf6 && number >= 0 && (nextTransition.num8 && !(nextId > 200 && nextTransition.num32) || number < 0x20 && !nextTransition.num32)) {
2291
+ transition = nextTransition.num8 || createTypeTransition(nextTransition, NUMBER, 1);
2292
+ target[position++] = number;
2293
+ } else {
2297
2294
  transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
2295
+ targetView.setUint32(position, number, true);
2298
2296
  position += 4;
2299
- break;
2297
+ }
2298
+ break;
2299
+ } else if (number < 0x100000000 && number >= -0x80000000) {
2300
+ targetView.setFloat32(position, number, true);
2301
+ if (float32Headers[target[position + 3] >>> 5]) {
2302
+ let xShifted;
2303
+ // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
2304
+ if (((xShifted = number * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
2305
+ transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
2306
+ position += 4;
2307
+ break;
2308
+ }
2300
2309
  }
2301
2310
  }
2302
2311
  }