I think this might help you - I got a speed up of about 4-5 times:
I will add my complete example here:
Future<ServerSocket> future = ServerSocket.bind("127.0.0.1", 1000);
future.then((ServerSocket sock) {
HttpServer s = new HttpServer.listenOn(sock);
s.listen((HttpRequest req) {
req.response.statusCode = HttpStatus.OK;
req.response.headers.contentType = ContentType.parse("image/png");
var file = new File("someImage.png");
// Average of about 5-7ms
Future f = file.readAsBytes();
req.response.addStream(f.asStream()).whenComplete(() {
req.response.close();
});
// Average of ~25-30ms
/*
file.readAsBytes().then((List<int> bytes) {
bytes.forEach((int b) => req.response.writeCharCode(b)); // slow!
req.response.close();
});
*/
});
});
Does this resolve your problem?
Regards Robert