8000 Optimize loop variables, minimize scope and use const · HowProgrammingWorks/Graph@99ca5bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 99ca5bf

Browse files
Optimize loop variables, minimize scope and use const
1 parent d6bc173 commit 99ca5bf

File tree

3 files changed

+30
-40
lines changed

3 files changed

+30
-40
lines changed

JavaScript/1-graph.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ class Vertex {
1010
const distinct = new Set(args);
1111
const links = this.links;
1212
const keyField = this.graph.keyField;
13-
let item, key;
14-
for (item of distinct) {
15-
key = item.data[keyField];
13+
for (const item of distinct) {
14+
const key = item.data[keyField];
1615
links.set(key, null);
1716
}
1817
return this;
@@ -26,10 +25,9 @@ class Cursor {
2625
linked(...names) {
2726
const vertices = this.vertices;
2827
const result = new Set();
29-
let vertex, condition, name;
30-
for (vertex of vertices) {
31-
condition = true;
32-
for (name of names) {
28+
for (const vertex of vertices) {
29+
let condition = true;
30+
for (const name of names) {
3331
condition = condition && vertex.links.has(name);
3432
}
3533
if (condition) result.add(vertex);
@@ -53,12 +51,11 @@ class Graph {
5351
}
5452
select(query) {
5553
const vertices = new Set();
56-
let vertex, condition, data, field;
57-
for (vertex of this.vertices.values()) {
58-
condition = true;
59-
data = vertex.data;
54+
for (const vertex of this.vertices.values()) {
55+
let condition = true;
56+
const data = vertex.data;
6057
if (data) {
61-
for (field in query) {
58+
for (const field in query) {
6259
condition = condition && data[field] === query[field];
6360
}
6461
if (condition) vertices.add(vertex);

JavaScript/2-insert-link-to.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ class Vertex {
1010
const distinct = new Set(args);
1111
const links = this.links;
1212
const keyField = this.graph.keyField;
13-
let item, key;
14-
for (item of distinct) {
15-
key = item.data[keyField];
13+
for (const item of distinct) {
14+
const key = item.data[keyField];
1615
links.set(key, null);
1716
}
1817
return this;
@@ -26,10 +25,9 @@ class Cursor {
2625
linked(...names) {
2726
const vertices = this.vertices;
2827
const result = new Set();
29-
let vertex, condition, name;
30-
for (vertex of vertices) {
31-
condition = true;
32-
for (name of names) {
28+
for (const vertex of vertices) {
29+
let condition = true;
30+
for (const name of names) {
3331
condition = condition && vertex.links.has(name);
3432
}
3533
if (condition) result.add(vertex);
@@ -53,12 +51,11 @@ class Graph {
5351
}
5452
select(query) {
5553
const vertices = new Set();
56-
let vertex, condition, data, field;
57-
for (vertex of this.vertices.values()) {
58-
condition = true;
59-
data = vertex.data;
54+
for (const vertex of this.vertices.values()) {
55+
let condition = true;
56+
const data = vertex.data;
6057
if (data) {
61-
for (field in query) {
58+
for (const field in query) {
6259
condition = condition && data[field] === query[field];
6360
}
6461
if (condition) vertices.add(vertex);

JavaScript/3-index.js

Lines changed: 12 additions & 16 deletions
341A
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ class Vertex {
1010
const distinct = new Set(args);
1111
const links = this.links;
1212
const keyField = this.graph.keyField;
13-
let item, key;
14-
for (item of distinct) {
15-
key = item.data[keyField];
13+
for (const item of distinct) {
14+
const key = item.data[keyField];
1615
links.set(key, null);
1716
}
1817
return this;
@@ -26,10 +25,9 @@ class Cursor {
2625
linked(...names) {
2726
const vertices = this.vertices;
2827
const result = new Set();
29-
let vertex, condition, name;
30-
for (vertex of vertices) {
31-
condition = true;
32-
for (name of names) {
28+
for (const vertex of vertices) {
29+
let condition = true;
30+
for (const name of names) {
3331
condition = condition && vertex.links.has(name);
3432
}
3533
if (condition) result.add(vertex);
@@ -54,12 +52,11 @@ class Graph {
5452
}
5553
select(query) {
5654
const vertices = new Set();
57-
let vertex, condition, data, field;
58-
for (vertex of this.vertices.values()) {
59-
condition = true;
60-
data = vertex.data;
55+
for (const vertex of this.vertices.values()) {
56+
let condition = true;
57+
const data = vertex.data;
6158
if (data) {
62-
for (field in query) {
59+
for (const field in query) {
6360
condition = condition && data[field] === query[field];
6461
}
6562
if (condition) vertices.add(vertex);
@@ -92,10 +89,9 @@ class Graph {
9289
idx = new Map();
9390
this.indices.set(key, idx);
9491
}
95-
let vertex, value, records;
96-
for (vertex of this.vertices.values()) {
97-
value = vertex.data[key];
98-
records = idx.get(value);
92+
for (const vertex of this.vertices.values()) {
93+
const value = vertex.data[key];
94+
let records = idx.get(value);
9995
if (!records) {
10096
records = new Set();
10197
idx.set(value, records);

0 commit comments

Comments
 (0)
0