Skip to content

Conversation

@oufeng
Copy link

@oufeng oufeng commented Sep 7, 2019

Optimize the performance of insert sort

The main problem

If currentIndex is equal to 0, you can abort while

validation

1: Create the test file

touch -p src/algorithms/sorting/insertion-sort/__test__/Performance.test.js

2: The file writes the following

import InsertionSort from "../InsertionSort";

function PerformanceTesting() {
  function initArray() {
    let array = [];
    const max = Math.pow(10, 4.5);
    for (let i = max; i > 0; i--) {
      array.push(i);
    }
    return array;
  }

  const sorter = new InsertionSort();
  const array = initArray();

  let start = Date.now();
  sorter.sort(array);

  const ms = Date.now() - start;
  return ms;
}

describe("PerformanceTesting", () => {
  it("PerformanceTesting times", () => {
    const InsertionSortMs = PerformanceTesting(InsertionSort);
    console.log(`InsertionSort times: ${InsertionSortMs}ms`);
  });
});

3: The results

Has not changed: 6007ms

After the changes: 1729ms

@oufeng oufeng closed this Sep 7, 2019
@oufeng oufeng reopened this Sep 7, 2019
@oufeng oufeng closed this Sep 7, 2019
@oufeng oufeng reopened this Sep 7, 2019
@codecov
Copy link

codecov bot commented Sep 7, 2019

Codecov Report

Merging #389 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #389   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         149    149           
  Lines        2612   2612           
  Branches      434    434           
=====================================
  Hits         2612   2612
Impacted Files Coverage Δ
...algorithms/sorting/insertion-sort/InsertionSort.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc1047d...e8d691e. Read the comment docs.

1 similar comment
@codecov
Copy link

codecov bot commented Sep 7, 2019

Codecov Report

Merging #389 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #389   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         149    149           
  Lines        2612   2612           
  Branches      434    434           
=====================================
  Hits         2612   2612
Impacted Files Coverage Δ
...algorithms/sorting/insertion-sort/InsertionSort.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc1047d...e8d691e. Read the comment docs.

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.

1 participant