Performance
Runnable MapLibre GL JS example: Performance. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:性能优化
核心逻辑:8000 点同数据分别走 heatmap 与 circle,实时 FPS 表 + setData 耗时对比
依赖:MapLibre GL JS 4 · jsDelivr CDN · demotiles 样式
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>性能优化</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
.bar{flex:0 0 auto;display:flex;gap:12px;align-items:center;padding:8px 12px;
background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar b{color:#16181d;font-weight:600}
.bar select{font:12px/1.5 inherit;padding:2px 6px}
.btn{border:1px solid #cdd3db;background:#fff;border-radius:4px;padding:3px 9px;
font:12px/1.5 inherit;color:#16181d;cursor:pointer}
#map{flex:1 1 auto;min-height:0}
</style>
</head>
<body>
<div class="bar">
<label>渲染方式
<select id="mode">
<option value="heat">heatmap</option>
<option value="circle">circle × 8000</option>
</select>
</label>
<button class="btn" id="rebuild">重建数据</button>
<span>FPS <b id="fps">—</b></span>
<span>点数 <b>8000</b></span>
<span>setData <b id="ms">—</b></span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.js"></script>
<script>
(function () {
'use strict';
if (typeof maplibregl === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#b42318">MapLibre 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var N = 8000;
function build(n) {
var features = new Array(n);
for (var i = 0; i < n; i++) {
features[i] = {
type: 'Feature',
properties: { v: Math.random() },
geometry: {
type: 'Point',
coordinates: [
100 + Math.random() * 22,
27 + Math.random() * 16
]
}
};
}
return { type: 'FeatureCollection', features: features };
}
var map = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [111, 34],
zoom: 3.6,
attributionControl: { compact: true, customAttribution: 'MapLibre demo tiles' }
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
function setMode(mode) {
if (!map.getLayer('heat') || !map.getLayer('dots')) return;
map.setLayoutProperty('heat', 'visibility', mode === 'heat' ? 'visible' : 'none');
map.setLayoutProperty('dots', 'visibility', mode === 'circle' ? 'visible' : 'none');
}
map.on('load', function () {
map.addSource('pts', { type: 'geojson', data: build(N) });
map.addLayer({
id: 'heat',
type: 'heatmap',
source: 'pts',
paint: {
'heatmap-radius': ['interpolate', ['linear'], ['zoom'], 2, 12, 6, 26],
'heatmap-intensity': 0.9,
'heatmap-opacity': 0.75
}
});
map.addLayer({
id: 'dots',
type: 'circle',
source: 'pts',
layout: { visibility: 'none' },
paint: {
'circle-radius': 2,
'circle-color': '#1f5fd0',
'circle-opacity': 0.65
}
});
});
document.getElementById('mode').addEventListener('change', function () {
setMode(this.value);
});
document.getElementById('rebuild').addEventListener('click', function () {
var src = map.getSource('pts');
if (!src) return;
var t0 = performance.now();
src.setData(build(N));
var t1 = performance.now();
document.getElementById('ms').textContent = (t1 - t0).toFixed(1) + ' ms';
});
var frames = 0;
var mark = performance.now();
function loop(ts) {
frames++;
if (ts - mark >= 1000) {
document.getElementById('fps').textContent = frames;
frames = 0;
mark = ts;
}
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
map.on('error', function (e) {
if (e.error) console.warn('地图错误:' + e.error.message);
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.