本文實例講述了node省市區(qū)三級數(shù)據(jù)性能測評。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)專注于虎林網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供虎林營銷型網(wǎng)站建設,虎林網(wǎng)站制作、虎林網(wǎng)頁設計、虎林網(wǎng)站官網(wǎng)定制、微信小程序定制開發(fā)服務,打造虎林網(wǎng)絡公司原創(chuàng)品牌,更為您提供虎林網(wǎng)站排名全網(wǎng)營銷落地服務。
閑來無事,測試下node和egg
首先是數(shù)據(jù)庫,大概長這樣
然后是代碼
'use strict'; const Controller = require('egg').Controller; class HomeController extends Controller { async index() { const { ctx } = this; ctx.body = 'hi, egg'; } async city() { const { ctx } = this; console.time("sql") const provinces = await this.app.MySQL.select('provinces') const citys = await this.app.mysql.select('cities') const areas = await this.app.mysql.select('areas') console.timeEnd("sql") console.time('cal') provinces.forEach(province => { let provinceid = province.provinceid province.children = [] citys.forEach(city => { city.children = [] if (city.provinceid === provinceid) { province.children.push(city) } let cityid = city.cityid areas.forEach(area => { if (area.cityid === cityid) { city.children.push(area) } }) }) }) console.timeEnd('cal') const result = { status: 1, data: provinces, } ctx.body = result; } } module.exports = HomeController;
執(zhí)行時間:
接著改進
'use strict'; const Controller = require('egg').Controller; class HomeController extends Controller { async index() { const { ctx } = this; ctx.body = 'hi, egg'; } async city() { const { ctx } = this; console.time("sql") let provinces = await this.app.mysql.select('provinces') let citys = await this.app.mysql.select('cities') let areas = await this.app.mysql.select('areas') console.timeEnd("sql") console.time('cal') for (let i = 0, len = citys.length; i < len; i++) { let city = citys[i] city.children = [] let cityid = city.cityid for (let j = 0, len1 = areas.length; j < len1; j++) { let area = areas[j] if (area.cityid === cityid) { city.children.push(areas.splice(j, 1)[0]) len1-- j-- } } } provinces.forEach(province => { let provinceid = province.provinceid province.children = [] for (let i = 0, len = citys.length; i < len; i++) { let city = citys[i] if (city.provinceid === provinceid) { province.children.push(city) citys.splice(i, 1) len-- i-- } } }) console.timeEnd('cal') const result = { status: 1, data: provinces, } ctx.body = result; } } module.exports = HomeController;
本次優(yōu)化結果
可以看到,在組裝數(shù)據(jù)的過程中,時間縮短了近20倍!
后續(xù)版本繼續(xù)優(yōu)化,也歡迎有相關方面經(jīng)驗的大神留言探討,給出更好的方案。
希望本文所述對大家node.js程序設計有所幫助。