Skip to content

Conversation

constantank
Copy link

@constantank constantank commented Mar 24, 2021

…e by merge copy and cleanup operation into one.

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

@posva
Copy link
Member

posva commented Mar 26, 2021

For this kind of changes that impact performance, please provide a comparison of the two methods to prove this version is worth implementing instead of the existing one

deep wathcers will be fired even if primitive value is not chagned, which in my understanding is unnecessary. seems like deep condition only make sence when isObject(value) returns true.
cache index for callbacks and make code more readable and clean by merge cleanup and copy operation into one
@constantank constantank reopened this Mar 28, 2021
@constantank
Copy link
Author

constantank commented Mar 28, 2021

For this kind of changes that impact performance, please provide a comparison of the two methods to prove this version is worth implementing instead of the existing one

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app"></div>
<script>
    let callbacks = [];
    let time = 0;
    function flushCallbacks() {
        const copies = callbacks.slice(0);
        callbacks.length = 0;
        for (let i = 0; i < copies.length; i++) {
            copies[i]();
        }
    }

    let t0, t1;

    for (let i = 0; i < 100000; i++) {
        if (i === 0) {
            t0 = performance.now();
        }
        callbacks.push(() => time++);
        if (i % 10000 === 0) {
            flushCallbacks();
        }
        if (i === 99999) {
            t1 = performance.now();
            console.log(t1 - t0, "old flush");
        }
    }

    callbacks = [];

    function flushCallbacks1() {
        const currentLength = callbacks.length;
        const currentCallback = callbacks.splice(0);
        for (let i = 0; i < currentLength; i++) {
            currentCallback[i]();
        }
    }

    for (let i = 0; i < 100000; i++) {
        if (i === 0) {
            t0 = performance.now();
        }
        callbacks.push(() => time++);
        if (i % 10000 === 0) {
            flushCallbacks1();
        }
        if (i === 99999) {
            t1 = performance.now();
            console.log(t1 - t0, "new flush");
        }
    }

</script>
</body>
</html>

thanks for the reply , i wrote a demo in a html and tried it in the console bar in chrome 89, the changed version is a little faster then the origin version .

@constantank constantank changed the title refactor: cache index for callbacks, make code more clean and readabl… refactor: cache index for callbacks, remove redundant watcher Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants