濡備綍鎻愬崌浣庣璁惧鐨刉eb鎬ц兘锛熻瘯璇曡嚜閫傚簲鍔犺浇妯″紡

Posted 鍓嶇涔嬪穮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了濡備綍鎻愬崌浣庣璁惧鐨刉eb鎬ц兘锛熻瘯璇曡嚜閫傚簲鍔犺浇妯″紡相关的知识,希望对你有一定的参考价值。


浣滆€咃綔Addy Osmani
璇戣€咃綔鐜嬪己
缂栬緫锝滅帇鏂囧┃
浠讳綍鐢ㄦ埛閮藉彲鑳芥湁杩囪繍琛岀紦鎱㈢殑浣撻獙銆? 杩欎釜涓栫晫鐨勮澶囧崈宸竾鍒紝鈥滀竴绉嶈鏍尖€濊蛋澶╀笅鐨勪綋楠屽彲鑳藉苟涓嶆槸鍝噷閮借寰楅€氥€備竴浜涚綉绔欐弧瓒充簡浣跨敤楂樼璁惧鐨勭敤鎴凤紝浣嗗湪浣庣璁惧涓婂嵈鍙兘鍗″緱娌℃硶鐢?[1]銆?/span>鐗瑰埆鏄湪鏅€氱殑绉诲姩璁惧鍜屾闈㈢‖浠讹紝浠ュ強鏂板叴甯傚満鐨勪富娴佽澶囦笂灏や负鏄庢樉銆傞偅涔堟垜浠兘涓嶈兘璋冩暣浜や粯椤甸潰鐨勬柟寮忥紝鏇村ソ鍦伴€傚簲鐢ㄦ埛鐨勭害鏉熸潯浠跺憿锛燄煠?鑷€傚簲鍔犺浇涓嶄粎瑕佹牴鎹睆骞曞ぇ灏忓仛鍑哄搷搴旓紝杩樿閫傚簲瀹為檯鐨勮澶囩‖浠躲€?
鍦?Chrome 寮€鍙戣€呭嘲浼氱殑婕旇涓?[2]锛屾垜鍜?Facebook 鐨?Nate Schloss 璁ㄨ浜? 鑷€傚簲鍔犺浇妯″紡锛屽嵆锛?
  • 涓烘墍鏈夌敤鎴凤紙鍖呮嫭浣庣璁惧鐢ㄦ埛锛夋彁渚涘揩閫熺殑鏍稿績浣撻獙锛?
  • 濡傛灉鐢ㄦ埛鐨勭綉缁滃拰纭欢鏈夎冻澶熺殑澶勭悊鑳藉姏锛屽垯閫愭娣诲姞浠呴檺浜庨珮绔‖浠剁殑鍔熻兘銆?/strong>

杩欎娇鐢ㄦ埛鍙互鑾峰緱鏈€閫傚悎浠栦滑鑷韩绾︽潫鏉′欢鐨勪娇鐢ㄤ綋楠屻€?/p>

濡備綍鎻愬崌浣庣璁惧鐨刉eb鎬ц兘锛熻瘯璇曡嚜閫傚簲鍔犺浇妯″紡

鑷€傚簲鍔犺浇瑙i攣鐨勭敤鎴峰満鏅寘鎷細
  • 鍦ㄦ參閫熺綉缁滀笂鎻愪緵浣庤川閲忕殑鍥惧儚鍜岃棰戯紱
  • 浠呭湪楂橀€?CPU 涓婂姞杞介潪鍏抽敭鐨?javascript 浜や簰鍔熻兘锛?
  • 闄愬埗浣庣璁惧涓婂姩鐢荤殑甯ч€熺巼锛?
  • 閬垮厤鍦ㄤ綆绔澶囦笂杩涜绻侀噸鐨勮繍绠楋紱
  • 鍦ㄩ€熷害杈冩參鐨勮澶囦笂闃绘绗笁鏂硅剼鏈€?/p>

濡備綍鎻愬崌浣庣璁惧鐨刉eb鎬ц兘锛熻瘯璇曡嚜閫傚簲鍔犺浇妯″紡

閽堝鑷€傚簲鍔犺浇锛屾垜浠湁寰堝淇″彿鍙敤锛屽叿浣撳寘鎷細
  • 缃戠粶锛?/strong> 鐢ㄤ簬寰皟鏁版嵁浼犺緭浠ュ噺灏戝甫瀹藉崰鐢紙閫氳繃 navigator.connection.effectiveType[3]锛夛紝杩樺彲浠ュ埄鐢ㄧ敤鎴风殑鈥滄祦閲忚妭鐪佺▼搴忊€濋閫夐」锛堥€氳繃 navigator.connection.saveData[4]锛夈€?/p>

  • 鍐呭瓨锛?/strong> 鐢ㄤ簬鍑忓皯浣庣璁惧涓婄殑鍐呭瓨娑堣€楋紙閫氳繃 navigator.deviceMemory[5]锛夈€?/p>

  • CPU 鏍稿績鏁帮細 鐢ㄤ簬闄愬埗寮€閿€杈冨ぇ鐨?JavaScript 鎵ц锛屽苟鍦ㄨ澶囧鐞嗚兘鍔涗笉瓒虫椂鍑忓皯 CPU 瀵嗛泦鍨嬮€昏緫锛堥€氳繃 navigator.hardwareConcurrency[6]锛夈€傝繖鏄洜涓?JavaScript 鐨勬墽琛屼富瑕佸彈 CPU 鎬ц兘绾︽潫 [7]銆?/p>

鍦ㄦ紨璁蹭腑锛屾垜浠粙缁嶄簡鍦?Facebook銆乪Bay 鍜?Tinder 绛夌綉绔欎笂搴旂敤杩欎簺鐞嗗康鐨勭湡瀹炴渚嬨€傚彲浠ヤ粠婕旇绗?24 鍒嗛挓寮€濮嬬湅璧凤紝Nate 浠嬬粛浜?Facebook 濡備綍鍦ㄧ敓浜х幆澧冧腑閫氳繃璁惧鍒嗙粍鏉ュ疄璺佃繖涓€鐞嗗康锛?/p>

濡備綍鎻愬崌浣庣璁惧鐨刉eb鎬ц兘锛熻瘯璇曡嚜閫傚簲鍔犺浇妯″紡

鎴戜滑杩樺彂甯冧簡涓€缁勬柊鐨勶紙瀹為獙鎬э級React Hooks 鍜屽疄鐢ㄥ伐鍏?[8]锛屽彲浠ュ湪浣犵殑 React 搴旂敤绋嬪簭涓坊鍔犺嚜閫傚簲鍔犺浇鎶€鏈€?/p>

鎴戜滑鐨?hooks/ 瀹炵敤宸ュ叿鍖呮嫭 useNetworkStatus React hook锛岀敤鏉ユ牴鎹綉缁滅姸鎬侊紙鏈夋晥鐨勮繛鎺ョ被鍨嬶級鍋氬嚭璋冩暣閫傚簲锛?
import React from 'react';

import { useNetworkStatus } from 'react-adaptive-hooks/network';

const MyComponent = () => {
  const { effectiveConnectionType } = useNetworkStatus();

  let media;
  switch(effectiveConnectionType) {
    case '2g':
      media = <img src='medium-res.jpg'/>;
      break;
    case '3g':
      media = <img src='high-res.jpg'/>;
      break;
    case '4g':
      media = <video muted controls>...</video>;
      break;
    default:
      media = <video muted controls>...</video>;
      break;
  }

  return <div>{media}</div>;
};
杩樻湁 useSaveData 瀹炵敤宸ュ叿锛岀敤鏉ユ牴鎹敤鎴锋祻瑙堝櫒鐨勬祦閲忚妭鐪佺▼搴忛閫夐」杩涜璋冩暣閫傚簲锛?
import React from 'react';

import { useSaveData } from 'react-adaptive-hooks/save-data';

const MyComponent = () => {
  const { saveData } = useSaveData();
  return (
    <div>
      { saveData ? <img src='...' /> : <video muted controls>...</video> }
    </div>
  );
};
鈥︹€︿互鍙?useHardwareConcurrency 瀹炵敤宸ュ叿锛岀敤鏉ユ牴鎹敤鎴疯澶囦笂鐨?CPU 澶勭悊鍣ㄩ€昏緫鍐呮牳鏁拌繘琛岃皟鏁撮€傚簲锛?
import React from 'react';

import { useHardwareConcurrency } from 'react-adaptive-hooks/hardware-concurrency';

const MyComponent = () => {
  const { numberOfLogicalProcessors } = useHardwareConcurrency();
  return (
    <div>
      { numberOfLogicalProcessors <= 4 ? <img src='...' /> : <video muted controls>...</video> }
    </div>
  );
};

鏈潵锛屾垜浠笇鏈涚湅鍒版洿澶氱殑鍩虹鏋舵瀯绀轰緥锛屽彲浠ユ牴鎹敤鎴风殑缃戠粶鍜岃澶囩害鏉熻嚜鍔ㄦ彁渚涙渶浼樺寲鐨勪唬鐮佸寘銆傚湪涓婇潰鎻愬埌鐨勮繖浜涘鎴风鎻愮ず [9] 鍜屽鎴风 API 涓棿锛屽彲鑳藉凡缁忔湁涓€浜涙瀯寤哄潡鍙敤鏉ュ湪杩欎釜棰嗗煙涓瀯寤哄嚭涓€浜涘紩浜烘敞鐩殑鎴愭灉浜嗐€?/p>

濡備綍鎻愬崌浣庣璁惧鐨刉eb鎬ц兘锛熻瘯璇曡嚜閫傚簲鍔犺浇妯″紡

鎴戜滑甯屾湜鑷€傚簲鍔犺浇鑳藉鎴愪负娓愯繘澧炲己宸ュ叿绠变腑涓€椤规湁鐢ㄧ殑鏂板伐鍏枫€傚叿浣撹鏌ョ湅婕旇浠ヤ簡瑙f洿澶氫俊鎭€?/p>

浜嗚В鏇村鍐呭锛?
  • React 鑷€傚簲鍔犺浇 hooks 鍜屽疄鐢ㄥ伐鍏凤細https://github.com/GoogleChromeLabs/react-adaptive-hooks
  • Tinder Web 宸ョ▼浣跨敤鐨勫鏉傝嚜閫傚簲鍔犺浇绛栫暐锛歨ttps://medium.com/@roderickhsiao/sophisticated-adaptive-loading-strategies-7118341fcf91
  • Angular 鐨勮繛鎺ユ劅鐭ョ粍浠讹細https://netbasal.com/connection-aware-components-in-angular-3a66bb0bab6f
  • 鍦?Vue 鍜?Web 缁勪欢涓紝浣跨敤缃戠粶淇℃伅 API 涓鸿嚜閫傚簲缁勪欢鎻愪緵鏈嶅姟锛歨ttps://dev.to/vorillaz/serving-adaptive-components-using-the-network-information-api-lbo
  • 鍩轰簬缃戠粶璐ㄩ噺鐨勮嚜閫傚簲鏈嶅姟锛歨ttps://web.dev/adaptive-serving-based-on-network-quality/

鐩稿叧閾炬帴
[1] https://v8.dev/blog/cost-of-javascript-2019
[2] https://www.youtube.com/watch?v=puUPpVrIRkc
[3] https://developer.mozilla.org/en-US/docs/Web/API/Network_Information_API
[4] https://developers.google.com/web /fundamentals/performance/optimizing-content-efficiency/save-data#detecting_the_save-data_setting
[5] https://developer.mozilla.org/en-US/docs/Web/API/Navigator/deviceMemory
[6] https://developer.mozilla.org/en-US/docs/Web/API/NavigatorConcurrentHardware/hardwareConcurrency
[7] https://www.youtube.com/watch?v=puUPpVrIRkc
[8] https://github.com/GoogleChrome Labs/react-adaptive-hooks
[9] https://developers.google.com/web/ fundamentals/performance/optimizing-content-efficiency/client-hints
浣滆€呬粙缁?

Addy Osmani 鏄?Chrome 鍥㈤槦鐨勫伐绋嬩富绠★紝鑷村姏浜庡垱閫犳洿蹇殑 Web 浣撻獙銆?/p>

鍘熸枃閾炬帴锛歨ttps://dev.to/addyosmani/adaptive-loading-improving-web-performance-on-low-end-devices-1m69

娲诲姩鎺ㄨ崘

鎶€鏈湪浼佷笟鐨勫彂灞曚腑鎴愪负瓒婃潵瓒婇噸瑕佺殑椹卞姩鍔涳紝鍥犳浼佷笟鍦ㄧ爺鍙戝洟闃熸垚闀夸笂鐨勬姇鍏ユ棩鐩婂鍔犮€傛彁鍗囩爺鍙戝洟闃熸晥鑳姐€佹墦閫犲涔犲瀷缁勭粐锛屾垚涓鸿秺鏉ヨ秺澶氫紒涓氱殑涓€涓噸瑕佷娇鍛姐€備綔涓虹爺鍙戝洟闃熷涔犳垚闀垮钩鍙帮紝鑷?鏋佸鏃堕棿浼佷笟璐﹀彿 涓婄嚎浠ユ潵锛屽凡缁忔湇鍔¤秴杩?1000 瀹朵紒涓氥€?/p>

鍦ㄤ娇鐢ㄥ満鏅笂锛屼紒涓氭妸鏋佸鏃堕棿浣滀负 鍥㈤槦鍩瑰吇宸ュ叿銆佹柊鍛樺伐鍩硅宸ュ叿銆佷紭绉€鍛樺伐绂忓埄 绛夛紝鎻愰珮浜嗘妧鏈洟闃熺殑鈥滄垬鏂楀姏鈥濓紝鍚屾椂涔熸彁楂樹簡浼佷笟鍦ㄨ涓氫腑鐨勭珵浜夊姏銆?span>鎵爜鎴栫偣鍑烩€滈槄璇诲師鏂団€?/strong>鏌ョ湅鍗庝负銆佽礉澹炽€佹瘨 App 绛変紒涓氭槸濡備綍閫氳繃鏋佸鏃堕棿鎵撻€犲涔犲瀷缁勭粐鐨勨啌

以上是关于濡備綍鎻愬崌浣庣璁惧鐨刉eb鎬ц兘锛熻瘯璇曡嚜閫傚簲鍔犺浇妯″紡的主要内容,如果未能解决你的问题,请参考以下文章

EF Core 寮傛缂栫▼娉ㄦ剰瑕佺偣

濡備綍閫夎喘宸ヤ笟璺敱鍣紵2020骞村伐涓氳矾鐢卞櫒閫夎喘鎸囧崡_浜戞垚鐢靛瓙璁惧

鍒嗕韩 9 鏉?Webpack 浼樺寲绛栫暐

绉嬫嫑鎻愬墠鎵瑰凡鏉ワ紝涓囧瓧闀挎枃鏁欎綘濡備綍澧炲姞闈㈣瘯澶у巶鐨勬垚鍔熺巼馃敟

銆愮洿鎾€戞暣杞﹂┚涔樻€ц兘娴嬭瘯璇勪环瑙e喅鏂规

濡備綍灏嗘暟鎹粠MySQL/MongoDB涓縼绉昏嚦浜戝紑鍙戞暟鎹簱