juliangruber / juliangruber/proxy-clone

[Discuss] Add cache for all props after get(prop)?

Open
#12 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
128
Forks
14
PR merge metrics
No merged PRs in 30d

Description

I think, proxyClone associate `orgObj` and `newObj`.

In a sense, `newObj` depends on `orgObj`

```js
let orgObj = {
a: 'hello'
};
let newObj = proxyClone(orgObj);
console.log(newObj.a); // 'hello'
delete orgObj.a;
console.log(newObj.a); // undefined
```

> It's not a regular copy operation in some cases

### detail source code
https://github.com/juliangruber/proxy-clone/blob/8c67bbd2bc362a94a68ecc7811e11716511de2c4/index.js#L32-L43

### some code optimization

1. Determine whether cache exists before getting it, and remove or comment out line37

```js
- 35 | if (isObject(value)) {
+ 35 | if (isObject(value) && !override[name] ) { // determine cache xistence before
36 | value = proxyClone(value)
- 37 | // override[name] = value // remove this line
38 | }
```
2. Add cache to `override[prop]`
```js
+ 42 | override[name] = value // add cache
43 | return value
```
After ..., in proxyClone2
```js
let orgObj = {
a: 'hello',
b: 123
};
let newObj = proxyClone2(orgObj);

console.log(newObj.a); // 'hello' ; trigger exec get value from orgObj at first time

delete orgObj.a;
delete orgObj.b;

console.log(newObj.a); // 'hello'
console.log(newObj.b); // undefined
```
> The situation has improved a bit, but it still exists.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.