class Renderer {
        constructor(renderInfo = {}) {
            this.width = renderInfo.width ?? 256;
            this.height = renderInfo.height ?? 256;
            this.context = renderInfo.context;
        }

        render(scene, camera) {
            const { width, height, context } = this;

            if (context != undefined) {
                this.imageBuffer = context.createImageData(width, height);
                this.depthBuffer = new Float32Array(width * height).fill(1);
            }

            const cameraMatrix = camera.cameraMatrix;
            const lightDirection = normalize([0, 0, 1]);


            const toScreen = (v) => [
                (v[0] / v[3] + 1) * 0.5 * width,
                (v[1] / v[3] + 1) * 0.5 * height,
                v[2] / v[3]
            ];

            for (const mesh of scene.meshes) {
                const worldVertices = mesh.geometry.vertices.map(v =>
                    fast4x1mul(mesh.transformMatrix, [v[0], v[1], v[2], 1])
                );

                const viewVertices = worldVertices.map(v =>
                    fast4x1mul(cameraMatrix, [v[0], v[1], v[2], 1])
                );

                const worldNormals = mesh.geometry.vertexNormals.map(n =>
                    normalize(fast4x1mul(mesh.transformMatrix, [n[0], n[1], n[2], 0]))
                );

                for (const indices of mesh.geometry.indices) {
                    if(camera.backfaceCullingEnabled) {
                        const [wv0, wv1, wv2] = [
                            worldVertices[indices[0]],
                            worldVertices[indices[1]],
                            worldVertices[indices[2]]
                        ];
                        const faceNormal = computeNormal(wv0, wv1, wv2);
                        const camToTri = [
                            wv0[0] - camera.position[0],
                            wv0[1] - camera.position[1],
                            wv0[2] - camera.position[2]
                        ];

                        if (dotProduct(faceNormal, camToTri) <= 0) {
                            continue;
                        }
                    }

                    const [vv0, vv1, vv2] = [
                        viewVertices[indices[0]],
                        viewVertices[indices[1]],
                        viewVertices[indices[2]]
                    ];

                    const [n0, n1, n2] = [
                        worldNormals[indices[0]],
                        worldNormals[indices[1]],
                        worldNormals[indices[2]]
                    ];


                    let triangles = [[vv0, vv1, vv2]];
                    let normalSets = [[n0, n1, n2]];

                    for (const [axis, min, max] of [[0, -1, 1], [1, -1, 1], [2, 0, 1]]) {
                        const newTriangles = [];
                        const newNormals = [];
                        for (let i = 0; i < triangles.length; i++) {
                            const clipped = clipTriangle(triangles[i], normalSets[i], axis, min, max);
                            for (const [clippedVertices, clippedNormals] of clipped) {
                                newTriangles.push(clippedVertices);
                                newNormals.push(clippedNormals);
                            }
                        }
                        triangles = newTriangles;
                        normalSets = newNormals;
                    }

                    for (let i = 0; i < triangles.length; i++) {
                        const [cV0, cV1, cV2] = triangles[i];
                        const [nV0, nV1, nV2] = normalSets[i];

                        const [vv0, vv1, vv2] = [toScreen(cV0), toScreen(cV1), toScreen(cV2)];

                        const colorV0 = Math.max(0, dotProduct(nV0, lightDirection)) * 255;
                        const colorV1 = Math.max(0, dotProduct(nV1, lightDirection)) * 255;
                        const colorV2 = Math.max(0, dotProduct(nV2, lightDirection)) * 255;

                        this.fillTriangle(vv0, vv1, vv2, colorV0, colorV1, colorV2);
                    }
                }
            }

            context.putImageData(this.imageBuffer, 0, 0);
        }

        fillTriangle(vv0, vv1, vv2, color0, color1, color2) {
            const { width, height, imageBuffer, depthBuffer } = this;

            const sortedVertices = [vv0, vv1, vv2].sort((a, b) => a[1] - b[1]);
            const [vv0Sorted, vv1Sorted, vv2Sorted] = sortedVertices;

            function barycentric(px, py, v0, v1, v2) {
                const denom = (v1[1] - v2[1]) * (v0[0] - v2[0]) + (v2[0] - v1[0]) * (v0[1] - v2[1]);
                if (denom === 0) return [-1, -1, -1];

                const w0 = ((v1[1] - v2[1]) * (px - v2[0]) + (v2[0] - v1[0]) * (py - v2[1])) / denom;
                const w1 = ((v2[1] - v0[1]) * (px - v2[0]) + (v0[0] - v2[0]) * (py - v2[1])) / denom;
                const w2 = 1 - w0 - w1;

                return [w0, w1, w2];
            }

            const yStart = Math.max(0, Math.ceil(vv0Sorted[1]));
            const yEnd = Math.min(height - 1, Math.floor(vv2Sorted[1]));

            for (let y = yStart; y <= yEnd; y++) {
                let xStart = width, xEnd = 0;

                for (const [v1, v2] of [[vv0Sorted, vv1Sorted], [vv1Sorted, vv2Sorted], [vv2Sorted, vv0Sorted]]) {
                    if (y < Math.min(v1[1], v2[1]) || y > Math.max(v1[1], v2[1])) continue;
                    const t = (y - v1[1]) / (v2[1] - v1[1]);
                    const x = v1[0] + t * (v2[0] - v1[0]);
                    xStart = Math.min(xStart, x);
                    xEnd = Math.max(xEnd, x);
                }

                xStart = Math.max(0, Math.ceil(xStart));
                xEnd = Math.min(width - 1, Math.floor(xEnd));

                for (let x = xStart; x <= xEnd; x++) {
                    const [w0, w1, w2] = barycentric(x, y, vv0, vv1, vv2);
                    if (w0 < 0 || w1 < 0 || w2 < 0) continue;

                    let index = y * width + x;

                    const zInterpolated = 1 / (w0 / vv0[2] + w1 / vv1[2] + w2 / vv2[2]);

                    if (zInterpolated < depthBuffer[index]) {
                        depthBuffer[index] = zInterpolated;

                        const interpolatedColor = w0 * color0 + w1 * color1 + w2 * color2;
                        const shadedColor = [
                            interpolatedColor,
                            interpolatedColor,
                            interpolatedColor,
                            255
                        ];

                        index *= 4;
                        imageBuffer.data[index] = shadedColor[0];
                        imageBuffer.data[index + 1] = shadedColor[1];
                        imageBuffer.data[index + 2] = shadedColor[2];
                        imageBuffer.data[index + 3] = shadedColor[3];
                    }
                }
            }
        }

    }


Rewrite this code to be more optimized. Remove any redundancies