Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.

Commit f913f1d

Browse files
authored
express: small perf (#10594)
1 parent 71c8f1c commit f913f1d

6 files changed

Lines changed: 35 additions & 44 deletions

File tree

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
module.exports = {
22

33
sanititizeTotal : (total) => {
4-
let totalIterations;
5-
if (!total || typeof(total) != 'number') {
6-
totalIterations = 1;
7-
} else if(total < 501 && total > 0) {
8-
totalIterations = total;
9-
} else if (total > 500) {
10-
totalIterations = 500;
11-
} else {
12-
totalIterations = 1;
13-
}
14-
return totalIterations;
4+
return total > 500 ? 500 : (i | 0) || 1;
155
},
166

177
randomizeNum : () => {
18-
return Math.floor(Math.random() * 10000) + 1
8+
return ((Math.random() * 10000) | 0) + 1;
199
}
2010
}

frameworks/JavaScript/express/mongodb-app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ if (cluster.isPrimary) {
103103

104104
app.get('/mongoose-update', async (req, res) => {
105105
const queryCount = Math.min(parseInt(req.query.queries, 10) || 1, 500);
106-
const promises = [];
106+
const promises = new Array(queryCount);
107107

108108
for (let i = 1; i <= queryCount; i++) {
109-
promises.push(getUpdateRandomWorld());
109+
promises[i - 1] = getUpdateRandomWorld();
110110
}
111111

112112
res.setHeader("Server", "Express");

frameworks/JavaScript/express/mysql-app.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ if (cluster.isPrimary) {
6464

6565
// Routes
6666
app.get('/mysql-orm-query', async (req, res) => {
67-
const results = [],
68-
queries = Math.min(parseInt(req.query.queries) || 1, 500);
67+
68+
const queries = Math.min(parseInt(req.query.queries) || 1, 500);
69+
const results = new Array(queries);
6970

7071
for (let i = 1; i <= queries; i++) {
7172
const world = await World.findOne({
@@ -74,7 +75,7 @@ if (cluster.isPrimary) {
7475
}
7576
}
7677
);
77-
results.push(world);
78+
results[i - 1] = world;
7879
}
7980

8081
res.setHeader("Content-Type", "application/json");
@@ -95,20 +96,19 @@ if (cluster.isPrimary) {
9596
res.send(world)
9697
});
9798

98-
app.get('/mysql-orm-fortune', (req, res) => {
99-
Fortune.findAll().then((fortunes) => {
100-
const newFortune = { id: 0, message: "Additional fortune added at request time." };
101-
fortunes.push(newFortune);
102-
fortunes.sort((a, b) => (a.message < b.message) ? -1 : 1);
99+
app.get('/mysql-orm-fortune', async(req, res) => {
100+
const fortunes = await Fortune.findAll()
101+
const newFortune = { id: 0, message: "Additional fortune added at request time." };
102+
fortunes.push(newFortune);
103+
fortunes.sort((a, b) => (a.message < b.message) ? -1 : 1);
103104

104-
res.setHeader("Server", "Express");
105-
res.render('fortunes/index', { fortunes: fortunes });
106-
});
105+
res.setHeader("Server", "Express");
106+
res.render('fortunes/index', { fortunes: fortunes });
107107
});
108108

109109
app.get('/mysql-orm-update', async (req, res) => {
110-
const results = [],
111-
queries = Math.min(parseInt(req.query.queries) || 1, 500);
110+
const queries = Math.min(parseInt(req.query.queries) || 1, 500);
111+
const results = new Array(queries);
112112

113113
for (let i = 1; i <= queries; i++) {
114114
const world = await World.findOne({
@@ -119,7 +119,7 @@ if (cluster.isPrimary) {
119119
);
120120
world.randomNumber = ~~(Math.random() * 10000) + 1;
121121
await world.save();
122-
results.push(world);
122+
results[i - 1] = world;
123123
}
124124

125125
res.setHeader("Server", "Express");

frameworks/JavaScript/express/resolver-mongo.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ async function getRandomWorld() {
3333

3434
function arrayOfRandomWorlds(totalWorldsToReturn) {
3535
const totalIterations = helper.sanititizeTotal(totalWorldsToReturn);
36-
const promises = [];
36+
const promises = new Array(totalIterations);
3737

3838
for (let i = 1; i <= totalIterations; i++) {
39-
promises.push(getRandomWorld());
39+
promises[i - 1] = getRandomWorld();
4040
}
4141

4242
return Promise.all(promises);
@@ -58,10 +58,10 @@ async function getAndUpdateRandomWorld() {
5858

5959
function updateRandomWorlds(totalToUpdate) {
6060
const totalIterations = helper.sanititizeTotal(totalToUpdate);
61-
const promises = [];
61+
const promises = new Array(totalIterations);
6262

6363
for (let i = 1; i <= totalIterations; i++) {
64-
promises.push(getAndUpdateRandomWorld());
64+
promises[i - 1] = getAndUpdateRandomWorld();
6565
}
6666

6767
return Promise.all(promises);

frameworks/JavaScript/express/resolver-postgres.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ const db = pgp(`postgres://${connection.username}:${connection.password}@${conne
1616
function arrayOfRandomWorlds(totalWorldsToReturn) {
1717

1818
const totalIterations = helper.sanititizeTotal(totalWorldsToReturn);
19-
const arr = [];
19+
const arr = new Array(totalIterations);
2020

2121
for(let i = 0; i < totalIterations; i++) {
22-
arr.push(getRandomWorld());
22+
arr[i] = getRandomWorld();
2323
}
2424

2525
return Promise.all(arr);
@@ -28,10 +28,10 @@ function arrayOfRandomWorlds(totalWorldsToReturn) {
2828
function updateRandomWorlds(totalToUpdate) {
2929

3030
const total = helper.sanititizeTotal(totalToUpdate);
31-
const arr = [];
31+
const arr = new Array(total);
3232

3333
for(let i = 0; i < total; i++) {
34-
arr.push(updateRandomWorld());
34+
arr[i] = updateRandomWorld();
3535
}
3636

3737
return Promise.all(arr);

frameworks/JavaScript/express/resolver.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ const Fortune = sequelize.define('fortune', {
5151
function arrayOfRandomWorlds(totalWorldToReturn) {
5252

5353
const totalIterations = helper.sanititizeTotal(totalWorldToReturn);
54-
const arr = [];
54+
const arr = new Array();
5555

5656
for(let i = 0; i < totalIterations; i++) {
57-
arr.push(World.findByPk(helper.randomizeNum()));
57+
arr[i] = World.findByPk(helper.randomizeNum());
5858
}
5959

6060
return Promise.all(arr);
@@ -63,19 +63,20 @@ function arrayOfRandomWorlds(totalWorldToReturn) {
6363
async function updateRandomWorlds(totalToUpdate) {
6464

6565
const total = helper.sanititizeTotal(totalToUpdate);
66-
const arr = [];
66+
const arr = new Array(total);
6767

6868
for(let i = 0; i < total; i++) {
69-
arr.push(World.findByPk(helper.randomizeNum()));
69+
arr[i] = World.findByPk(helper.randomizeNum());
7070
}
7171

7272
const results = await Promise.all(arr);
7373

74-
const updates = [];
75-
for(const world of results){
76-
updates.push(world.updateAttributes({
74+
const updates = new Array(total);
75+
for (let index = 0; index < results.length; index++) {
76+
const element = results[index];
77+
updates[index] = element.updateAttributes({
7778
randomNumber: helper.randomizeNum()
78-
}));
79+
});
7980
}
8081

8182
await Promise.all(updates);

0 commit comments

Comments
 (0)