Skip to content

Commit

Permalink
feat(Object): add buffers, errors, regexp cloning support in clone fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed Mar 27, 2024
1 parent 3e966e7 commit 7bbe29a
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-peas-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opentf/std": minor
---

Added buffers, errors, regex support for clone fn.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,23 @@ clone:
┌───┬──────────────────────────┬─────────┬────────────────────┬────────┬─────────┐
│ │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├───┼──────────────────────────┼─────────┼────────────────────┼────────┼─────────┤
│ 0 │ structuredClone (Native) │ 246,358 │ 4059.129038804869 │ ±1.65% │ 24636 │
│ 1 │ _.cloneDeep (Lodash) │ 169,432 │ 5902.043319168882 │ ±2.53% │ 16944 │
│ 2 │ R.clone (ramda) │ 178,957 │ 5587.9189808343945 │ ±2.26% │ 17897 │
│ 3 │ R2.clone (remeda) │ 314,247 │ 3182.204862216371 │ ±1.75% │ 31426 │
│ 4 │ clone │ 399,634 │ 2502.2859323385046 │ ±2.06% │ 39964 │
│ 0 │ structuredClone (Native) │ 276,824 │ 3612.3959469709525 │ ±1.29% │ 27683 │
│ 1 │ _.cloneDeep (Lodash) │ 216,965 │ 4609.032953864744 │ ±2.41% │ 21697 │
│ 2 │ R.clone (ramda) │ 174,567 │ 5728.439422580611 │ ±1.92% │ 17457 │
│ 3 │ R2.clone (remeda) │ 310,268 │ 3223.0154703960834 │ ±2.40% │ 31027 │
│ 4 │ cloneDeep (clone-deep) │ 468,908 │ 2132.611673882092 │ ±1.70% │ 46891 │
│ 5 │ copy (fast-copy) │ 486,179 │ 2056.852050680814 │ ±1.91% │ 48618 │
│ 6 │ clone │ 535,302 │ 1868.1028376072306 │ ±2.07% │ 53531 │
└───┴──────────────────────────┴─────────┴────────────────────┴────────┴─────────┘
*Note: Here the ramda & remeda does not support cloning Map & Set.
*Note:
- Here the lodash does not support errors, sparse arrays & objects in map keys.

- Here the ramda & remeda does not support cloning Map & Set.

- The fast-copy does not clone objects within Map, buffers in TypedArray, sparse arrays.

- The clone-deep does not handle circular refs, does not clone objects within map,
sparse arrays, internal refs within the object, TypedArray buffers & DataView.

sortBy:
┌───┬────────────────────┬───────────┬───────────────────┬────────┬─────────┐
Expand Down
37 changes: 24 additions & 13 deletions apps/docs/pages/Object/clone.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,33 @@ clone<T>(val: T): T;

<Callout type="info">
The following types are supported:
- Primitive values:
- Undefined
- Null
- Boolean
- Number
- BigInt
- String
- Object
- Array
- Map
- Set
- Date


- [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)

- [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)

- [DataView](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView)

- [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)

- [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)

- [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)

- [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) objects: but only plain objects (e.g. from object literals).

- [Primitive types,](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values) except symbol.

- [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp): but note that lastIndex is not preserved.

- [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)

- [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
</Callout>

<Callout type="info">
If you need more types to clone, please use the native [structuredClone](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) global function.
If you need more types to clone with transferable objects, please use the native [structuredClone](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) global function.
</Callout>

## Examples
Expand Down
21 changes: 15 additions & 6 deletions apps/docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,23 @@ clone:
┌───┬──────────────────────────┬─────────┬────────────────────┬────────┬─────────┐
│ │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├───┼──────────────────────────┼─────────┼────────────────────┼────────┼─────────┤
│ 0 │ structuredClone (Native) │ 246,358 │ 4059.129038804869 │ ±1.65% │ 24636 │
│ 1 │ _.cloneDeep (Lodash) │ 169,432 │ 5902.043319168882 │ ±2.53% │ 16944 │
│ 2 │ R.clone (ramda) │ 178,957 │ 5587.9189808343945 │ ±2.26% │ 17897 │
│ 3 │ R2.clone (remeda) │ 314,247 │ 3182.204862216371 │ ±1.75% │ 31426 │
│ 4 │ clone │ 399,634 │ 2502.2859323385046 │ ±2.06% │ 39964 │
│ 0 │ structuredClone (Native) │ 276,824 │ 3612.3959469709525 │ ±1.29% │ 27683 │
│ 1 │ _.cloneDeep (Lodash) │ 216,965 │ 4609.032953864744 │ ±2.41% │ 21697 │
│ 2 │ R.clone (ramda) │ 174,567 │ 5728.439422580611 │ ±1.92% │ 17457 │
│ 3 │ R2.clone (remeda) │ 310,268 │ 3223.0154703960834 │ ±2.40% │ 31027 │
│ 4 │ cloneDeep (clone-deep) │ 468,908 │ 2132.611673882092 │ ±1.70% │ 46891 │
│ 5 │ copy (fast-copy) │ 486,179 │ 2056.852050680814 │ ±1.91% │ 48618 │
│ 6 │ clone │ 535,302 │ 1868.1028376072306 │ ±2.07% │ 53531 │
└───┴──────────────────────────┴─────────┴────────────────────┴────────┴─────────┘
*Note:
- Here the lodash does not support errors, sparse arrays & objects in map keys.

- Here the ramda & remeda does not support cloning Map & Set.

- The fast-copy does not clone objects within Map, buffers in TypedArray, sparse arrays.

*Note: Here the ramda & remeda does not support cloning Map & Set.
- The clone-deep does not handle circular refs, does not clone objects within map,
sparse arrays, internal refs within the object, TypedArray buffers & DataView.

sortBy:
┌───┬────────────────────┬───────────┬───────────────────┬────────┬─────────┐
Expand Down
37 changes: 29 additions & 8 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import * as R from "ramda";
import * as R2 from "remeda";
import { sort as mSort } from "moderndash";
import fastDeepEqual from "fast-deep-equal/es6";
import { dequal } from 'dequal';
import { dequal } from "dequal";
import cloneDeep from "clone-deep";
import copy from "fast-copy";

async function cloneBench() {
console.log("clone:");
Expand Down Expand Up @@ -42,18 +44,29 @@ async function cloneBench() {
.add("R2.clone (remeda)", () => {
R2.clone(obj);
})
.add("cloneDeep (clone-deep)", () => {
cloneDeep(obj);
})
.add("copy (fast-copy)", () => {
copy(obj);
})
.add("clone", () => {
clone(obj);
})
.todo("unimplemented bench");
});

await bench.warmup();
await bench.run();

console.table(bench.table());
console.log(
"*Note: Here the ramda & remeda does not support cloning Map & Set."
);
console.log(`*Note:
- Here the lodash does not support errors, sparse arrays & objects in map keys.
- Here the ramda & remeda does not support cloning Map & Set.
- The fast-copy does not clone objects within Map, buffers in TypedArray, sparse arrays..
- The clone-deep does not handle circular refs, does not clone objects within map, sparse arrays, internal refs within the object, TypedArray buffers & DataView.
`);
}

async function sortByBench() {
Expand Down Expand Up @@ -149,8 +162,16 @@ async function isEqlBench() {
await bench.run();

console.table(bench.table());
console.log(`*Note:
- The native util deepStrictEqual not available in browsers, does not check Map ordering & same invalid dates.
- The fast-deep-equal/es6 does not support cyclic refs, Map ordering check, invalid dates, symbols, objects values in Set & TypedArrays.
- The lodash isEqual does not check Map ordering & object values in Set.
- The ramda equals does not check Map ordering & symbols.
- The dequal does not support cyclic refs, Map ordering, symbols & same invalid dates.
`);
}

// await cloneBench();
await cloneBench();
// await sortByBench();
await isEqlBench();
// await isEqlBench();
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"@changesets/cli": "^2.27.1",
"@commitlint/cli": "^18.6.0",
"@commitlint/config-conventional": "^18.6.0",
"clone-deep": "^4.0.1",
"dequal": "^2.0.3",
"fast-copy": "^3.0.2",
"fast-deep-equal": "^3.1.3",
"husky": "^9.0.10",
"lint-staged": "^15.2.2",
Expand All @@ -39,7 +42,6 @@
"ramda": "^0.29.1",
"remeda": "^1.48.0",
"tinybench": "^2.6.0",
"turbo": "^1.12.2",
"dequal": "^2.0.3"
"turbo": "^1.12.2"
}
}
22 changes: 16 additions & 6 deletions packages/std/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,23 @@ clone:
┌───┬──────────────────────────┬─────────┬────────────────────┬────────┬─────────┐
│ │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├───┼──────────────────────────┼─────────┼────────────────────┼────────┼─────────┤
│ 0 │ structuredClone (Native) │ 246,358 │ 4059.129038804869 │ ±1.65% │ 24636 │
│ 1 │ _.cloneDeep (Lodash) │ 169,432 │ 5902.043319168882 │ ±2.53% │ 16944 │
│ 2 │ R.clone (ramda) │ 178,957 │ 5587.9189808343945 │ ±2.26% │ 17897 │
│ 3 │ R2.clone (remeda) │ 314,247 │ 3182.204862216371 │ ±1.75% │ 31426 │
│ 4 │ clone │ 399,634 │ 2502.2859323385046 │ ±2.06% │ 39964 │
│ 0 │ structuredClone (Native) │ 276,824 │ 3612.3959469709525 │ ±1.29% │ 27683 │
│ 1 │ _.cloneDeep (Lodash) │ 216,965 │ 4609.032953864744 │ ±2.41% │ 21697 │
│ 2 │ R.clone (ramda) │ 174,567 │ 5728.439422580611 │ ±1.92% │ 17457 │
│ 3 │ R2.clone (remeda) │ 310,268 │ 3223.0154703960834 │ ±2.40% │ 31027 │
│ 4 │ cloneDeep (clone-deep) │ 468,908 │ 2132.611673882092 │ ±1.70% │ 46891 │
│ 5 │ copy (fast-copy) │ 486,179 │ 2056.852050680814 │ ±1.91% │ 48618 │
│ 6 │ clone │ 535,302 │ 1868.1028376072306 │ ±2.07% │ 53531 │
└───┴──────────────────────────┴─────────┴────────────────────┴────────┴─────────┘
*Note: Here the ramda & remeda does not support cloning Map & Set.
*Note:
- Here the lodash does not support errors, sparse arrays & objects in map keys.

- Here the ramda & remeda does not support cloning Map & Set.

- The fast-copy does not clone objects within Map, buffers in TypedArray, sparse arrays.

- The clone-deep does not handle circular refs, does not clone objects within map,
sparse arrays, internal refs within the object, TypedArray buffers & DataView.

sortBy:
┌───┬────────────────────┬───────────┬───────────────────┬────────┬─────────┐
Expand Down

0 comments on commit 7bbe29a

Please sign in to comment.