node省市區(qū)三級(jí)數(shù)據(jù)性能測(cè)評(píng)實(shí)例分析
本文實(shí)例講述了node省市區(qū)三級(jí)數(shù)據(jù)性能測(cè)評(píng)。分享給大家供大家參考,具體如下:
閑來無事,測(cè)試下node和egg
首先是數(shù)據(jù)庫,大概長(zhǎng)這樣
然后是代碼
'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í)行時(shí)間:
接著改進(jìn)
'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)化結(jié)果
可以看到,在組裝數(shù)據(jù)的過程中,時(shí)間縮短了近20倍!
后續(xù)版本繼續(xù)優(yōu)化,也歡迎有相關(guān)方面經(jīng)驗(yàn)的大神留言探討,給出更好的方案。
希望本文所述對(duì)大家node.js程序設(shè)計(jì)有所幫助。
- nodejs+mongodb aggregate級(jí)聯(lián)查詢操作示例
- Node.js+ES6+dropload.js實(shí)現(xiàn)移動(dòng)端下拉加載實(shí)例
- node微信開發(fā)之獲取access_token+自定義菜單
- 詳解nodejs微信公眾號(hào)開發(fā)——6.自定義菜單
- JavaScript NodeTree導(dǎo)航欄(菜單項(xiàng)JSON類型/自制)
- nodejs 實(shí)現(xiàn)模擬form表單上傳文件
- Nodejs之http的表單提交
- Nodejs下用submit提交表單提示cannot post錯(cuò)誤的解決方法
- 全面解析node 表單的圖片上傳
- Node層模擬實(shí)現(xiàn)multipart表單的文件上傳示例
- 推薦一個(gè)基于Node.js的表單驗(yàn)證庫
相關(guān)文章
Node.js學(xué)習(xí)之TCP/IP數(shù)據(jù)通訊(實(shí)例講解)
下面小編就為大家?guī)硪黄狽ode.js學(xué)習(xí)之TCP/IP數(shù)據(jù)通訊(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Express框架實(shí)現(xiàn)簡(jiǎn)單攔截器功能示例
這篇文章主要介紹了Express框架實(shí)現(xiàn)簡(jiǎn)單攔截器功能,結(jié)合實(shí)例形式分析了express框架攔截器相關(guān)功能與使用方法,需要的朋友可以參考下2023-05-05利用nodejs讀取圖片并將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換成base64格式
這篇文章主要介紹了利用nodejs讀取圖片并將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換成base64格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Node.js基礎(chǔ)入門之緩存區(qū)與文件操作詳解
Node.js是一個(gè)基于Chrome?V8引擎的JavaScript運(yùn)行時(shí)。類似于Java中的JRE,.Net中的CLR。本文將詳細(xì)為大家介紹Node.js中的緩存區(qū)與文件操作,感興趣的可以了解一下2022-03-03M2實(shí)現(xiàn)Nodejs項(xiàng)目自動(dòng)部署的方法步驟
這篇文章主要介紹了M2實(shí)現(xiàn)Nodejs項(xiàng)目自動(dòng)部署的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05Node.js中使用計(jì)時(shí)器定時(shí)執(zhí)行函數(shù)詳解
這篇文章主要介紹了Node.js中使用計(jì)時(shí)器定時(shí)執(zhí)行函數(shù)詳解,本文使用了Node.js中的setTimeout和setInterval函數(shù),需要的朋友可以參考下2014-08-08